Solved

Broken function after implementing PDO

Posted on 2014-03-13
5
358 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 110

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
The viewer will learn how to dynamically set the form action using jQuery.
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 …

724 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