0

Running the following query works perfectly in my user search, within mySQL on phpmyadmin.

SELECT user_id, fname, lname, email FROM users WHERE fname LIKE '%Scott James%' OR lname LIKE '%Scott James%' OR CONCAT(fname,' ',lname) LIKE '%Scott James%'

However, if I try and run this through php on production environment, it seems to fall over. Anyone got any ideas?

$word = $_REQUEST['search'];

$search_query='SELECT user_id, fname, lname, email FROM users WHERE fname LIKE :search OR lname LIKE :search OR CONCAT(fname,' ',lname) LIKE :search';

$stmt= $conn->prepare($search_query);
$stmt->execute(array(
  ':search'   => '%'.$word.'%',
  ':user_id'   => $user_id 
    ));
user3189734
  • 665
  • 1
  • 9
  • 27
  • 1. what is this `':user_id' => $user_id` thing? 2. Define "not working" AKA "fall over" in more technical terms – Your Common Sense Mar 24 '14 at 14:35
  • Add some dumps of information to help you. Wrap the execute in a try-catch and output the exception message, for example. – markdwhite Mar 24 '14 at 14:35
  • @markdwhite try-catch ISN't REQUIRED to get an exception. – Your Common Sense Mar 24 '14 at 14:36
  • @YourCommonSense - end result is: user needs to find out more information of the state of the script and provide it so we can help. Nothing is required. Some things are helpful. There are different ways of doing some things. – markdwhite Mar 24 '14 at 14:41
  • @markdwhite try-catch ISN'T helpful in debugging PDO, but rather harmful. If user needs more information he has to turn PHP reporting on (assuming exceptions already enabled in PDO) – Your Common Sense Mar 24 '14 at 14:42
  • 1
    Typo? You have `ROM` in `SELECT user_id, fname, lname, email ROM...` which should read as `FROM` replace with `SELECT user_id, fname, lname, email FROM... ` – Funk Forty Niner Mar 24 '14 at 14:55
  • According to this answer - http://stackoverflow.com/a/2445720/689579 and [PDO::prepare](http://php.net/manual/en/pdo.prepare.php) *you cannot use a named parameter marker of the same name twice in a prepared statement*, unless you are in emulation mode. – Sean Mar 24 '14 at 15:05

1 Answers1

1
  1. make sure PDO is in emulation mode to allow multiple parameters with same name
  2. Get rid of this ':user_id' => $user_id stuff.
  3. Make sure PDO is in exception mode and you can see PHP errors
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345