Solved

Broken function after implementing PDO

Posted on 2014-03-13
5
351 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:sjtinsley83
5 Comments
 
LVL 108

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:sjtinsley83
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

707 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

14 Experts available now in Live!

Get 1:1 Help Now