?
Solved

Broken function after implementing PDO

Posted on 2014-03-13
5
Medium Priority
?
360 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…
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.
Suggested Courses

771 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