Jump to content

code compare


cypher86
Go to solution Solved by mac_gyver,

Recommended Posts

i'm using jquery autocomplete with a remote php script.

this code works fine:

db_connect();

$term = trim(strip_tags($_GET['term']));//retrieve the search term that autocomplete sends

$qstring = "SELECT id,nominativo,indirizzo,telefono,fax,e_mail FROM legale WHERE nominativo LIKE '%".$term."%'";
$result = mysql_query($qstring);//query the database for entries containing the term

while ($row = mysql_fetch_array($result,MYSQL_ASSOC))//loop through the retrieved values
{
		$row['nominativo']=htmlentities(stripslashes($row['nominativo']));
		$row['indirizzo']=htmlentities(stripslashes($row['indirizzo']));
		$row['telefono']=htmlentities(stripslashes($row['telefono']));
		$row['fax']=htmlentities(stripslashes($row['fax']));
		$row['e_mail']=htmlentities(stripslashes($row['e_mail']));
		$row['id']=(int)$row['id'];
		$row_set[] = $row;//build an array
}
echo json_encode($row_set);//format the array into json data

this one doesn't:

$db=db_connect_param();

$term = trim(strip_tags($_GET['term']));//retrieve the search term that autocomplete sends

if($_GET['what']=="legale")
	$qstring = "SELECT id,nominativo,indirizzo,telefono,fax,e_mail FROM legale WHERE nominativo LIKE ?";

$search="%$term%";
$sql=$db->prepare($qstring);
$sql->bind_param('s',$search);
$sql->execute();
$sql->bind_result($id,$nominativo,$indirizzo,$telefono,$fax,$e_mail);

//build an array
while($sql->fetch()) {
	$row['nominativo']=htmlentities(stripslashes($nominativo));
	$row['indirizzo']=htmlentities(stripslashes($indirizzo));
	$row['telefono']=htmlentities(stripslashes($telefono));
	$row['fax']=htmlentities(stripslashes($fax));
	$row['e_mail']=htmlentities(stripslashes($e_mail));
	$row['id']=(int)$id;
	$row_set[]=$row; 
}
echo json_encode($row_set);//format the array into json data*/

what's the difference?

the json returned seems the same.

Link to comment
Share on other sites

this one doesn't:

 

 

telling us what sort of error or symptom you got that leads you to believe the code doesn't work, would help narrow down the problem.

 

the json returned seems the same.

 

 

if they are not identical, posting both of them would give someone here information upon which to help diagnose the problem.

 

it's also possible that the second code is outputting something prior to the json encoded data, such as some white-space, a php error message. you should be able to check in your browser's debugging console if there are errors or some unexpected/malformed data.

Link to comment
Share on other sites

  • 2 weeks later...

1 - do you have error checking turned properly so that errors can be shown to you?

 

2 - "this one doesn't". Just what does that mean? white screen? no json returned? something else?

 

3 - and what if $_GET['what'] is not equal to 'legale'? What query are you going to be preparing then?

Link to comment
Share on other sites

 

 

1 - do you have error checking turned properly so that errors can be shown to you?

yes

 

 

2 - "this one doesn't". Just what does that mean? white screen? no json returned? something else?

no error are diplayed, the json is returned and seems identical to the one returned from the first script

 

 

3 - and what if $_GET['what'] is not equal to 'legale'? What query are you going to be preparing then? 

then it returns nothing. since the json is returned i assume that the parameters are correctly set.

Link to comment
Share on other sites

  • Solution

two different people have asked you to tell us what sort of error/symptom or incorrect result you are getting that leads you to believe this isn't working. how do you know it is not working? we are not standing right next to you and can only see the information that you put in your posts  :psychic:

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.