Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8603821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:24:51+00:00 2026-06-12T02:24:51+00:00

I have the following code as part of a larger web application to search

  • 0

I have the following code as part of a larger web application to search for employees and return the required information. The queries themselves take little to no time to complete and return the result set. What appears to need some improvement is how I am currently encoding the results into an array for json_encode to return to the front end. I am out of ideas for how to improve upon the code (hence my question here). Any ideas would be greatly appreciated!

<?php
require_once("class.employee.php");
$employee = new Employee();
$employeeSearch = $employee->searchEmployees($_REQUEST['q']);
$employeeResults = array();
$row_array['id'] = $_REQUEST['q'];
$row_array['empName'] = $_REQUEST['q'];
$row_array['empBusinessTitle'] = '';
$row_array['empFacility'] = '';
array_push($employeeResults, $row_array);
while ($empInfo = $employeeSearch->fetchObject()) {
    $row_array['id'] = $empInfo->empUserName;
    $row_array['empName'] = ucwords($empInfo->empName);
    $row_array['empBusinessTitle'] = $empInfo->empBusinessTitle;
    $facilityName = $employee->getFacilityIDByAD($empInfo->empUserName);
    $row_array['empFacility'] = isset($facilityName->facilityName) ? $facilityName->facilityName : '';
    array_push($employeeResults, $row_array);
}
$ret['results'] = $employeeResults;
echo json_encode($ret);

Class Employee {
public function searchEmployees($query) {
    try {
        $dbh = new PDO($this->dbDSN, $this->dbUser, $this->dbPass);
        $statement = $dbh->prepare("SELECT empID, CONCAT(empFirstName,' ',empLastName) as empName, empUserName, empBusinessTitle from $this->tblEmployeePeople where CONCAT(empfirstname,' ',emplastName) LIKE CONCAT('%',:query,'%') and empUserName != ''");
        $statement->bindParam(':query', $query);
        $statement->execute();
        $dbh = null;
        return $statement;
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
}

}?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:24:52+00:00Added an answer on June 12, 2026 at 2:24 am

    The functions called in the where clause breaks any index you might have on these fields. Here is the worst part of your query from a performance standpoint.

    CONCAT(empfirstname,' ',emplastName) LIKE CONCAT('%',:query,'%')
    

    First, why the need for the second CONCAT? Just do this

     CONCAT(empfirstname,' ',emplastName) LIKE '%:query%'
    

    However, would still hit the index breaking penalty for the first Concat function. Also, why the wildcard Likes? You probably want an exact equality check.

    I would break the passed in request value ‘q’ down into 2 parts a first and last name, and pass that to the search function.

    Try this in the search function:

      $nameArray = explode('',$query );
      $firstname = $nameArray[0];
      $lastname  = $nameArray[1];
    

    Then change your query to be:

      WHERE ( empfirstname LIKE ':firstname' AND emplastname = ':lastname' ) AND empUserName != ''
    

    Then add the binding with wildcard like this:

       $statement->bindValue(':firstname', $firstname."%");
       $statement->bindValue(':firstname', $lastname."%");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using linkedin-j , I have the following code in one part of my application
I have the following vba code is part of a larger script. The issue
I have the following block of code that is part of a larger program.
I have the following code in one cpp file which is part of a
Ok - I have the following code - on the starred part I get
I have the following code, which is run every 10ms as part of a
I have the following Template Haskell code in my module, which is part of
As part of a large python program I have the following code: for arg
I have a following layout of my web page. Following CSS code gives me
I have following code in initialization im = imread('Image02.tif'); figure(); imagesc(im); colormap(gray); [hImage hfig

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.