Solved

Broken function after implementing PDO

Posted on 2014-03-13
5
354 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
5 Comments
 
LVL 109

Accepted Solution

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

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

813 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now