Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Broken function after implementing PDO

Posted on 2014-03-13
5
Medium Priority
?
361 Views
Last Modified: 2014-03-13
I have decided to upgrade my code to keep uptodate with mysql.
I have a function which worked fine before but now doesnt...

Firebug is giving this error:

GET http://localhost/stinteractive/touch/library/process_user.php?action=findUsers&term=ste      500 Internal Server Error      15ms      jquery....min.js (line 6)
"NetworkError: 500 Internal Server Error - http://localhost/stinteractive/touch/library/process_user.php?action=findUsers&term=ste"

Could someone help!

OLD
function findUsers()
{
//$text = mysql_real_escape_string($_GET['term']);
$text = $_GET['term'];

$query = "SELECT userID, userFirstname, userSurname FROM user WHERE CONCAT(userFirstname, ' ', userSurname) LIKE '%$text%' OR userSurname LIKE '%$text%' ORDER BY userSurname ASC";
$result = mysql_query($query);
$json = '[';
$first = true;


while($row = mysql_fetch_array($result)) {
    if (!$first) {
		$json .=  ',';
	} else {
		$first = false;
	}
    
	$json .= '{"userID":"'.$row['userID'].'",';
	$json .= '"value":"'.$row['userFirstname'] .' '.$row['userSurname'].'"}';
}
$json .= ']';
echo $json;
}

Open in new window



NEW (and doesnt work)
function findUsers()
{
	//$text = mysql_real_escape_string($_GET['term']);
	$text = $_GET['term'];

	try
	{
		$sql = $db->prepare('SELECT userID, userFirstname, userSurname
			FROM user
			WHERE CONCAT(userFirstname, " ", userSurname) LIKE ?
			OR userSurname LIKE ?
			ORDER BY userSurname ASC');
		$sql->execute(array("%".$text."%","%".$text."%"));

		$sql->setFetchMode(PDO::FETCH_ASSOC);
		$data = $sql->fetchAll();
	}

	catch(PDOException $e)
	{
		echo $e->getMessage();
		die;
	}

	$json = '[';
	$first = true;

	foreach ($data as $row) {

	    if (!$first) {
			$json .=  ',';
		} else {
			$first = false;
		}
	    
		$json .= '{"userID":"'.$row['userID'].'",';
		$json .= '"value":"'.$row['userFirstname'] .' '.$row['userSurname'].'"}';
	}
	$json .= ']';
	echo $json;

}

Open in new window

0
Comment
Question by:Steve Tinsley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 39927473
$db is undefined in the scope of the findusers() function.  Maybe make it a global?
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39927481
I think you forgot the ''

$sql = $db->prepare('SELECT userID, userFirstname, userSurname
			FROM user
			WHERE CONCAT(userFirstname, " ", userSurname) LIKE '?'
			OR userSurname LIKE '?'
			ORDER BY userSurname ASC');
		$sql->execute(array("%".$text."%","%".$text."%"))

Open in new window

HTH,
Dan
0
 
LVL 58

Expert Comment

by:Gary
ID: 39927489
...Or pass it to your function

@Dan
They are placeholders, you don't encapsulate them.
0
 

Author Comment

by:Steve Tinsley
ID: 39927608
This
global $db;
in the function fixed it!
0
 
LVL 58

Expert Comment

by:Gary
ID: 39927616
Then you should accept Rays comment and not mine
Re-opening.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question