I have two files here.... index.html and postback.php
index.html

<html>
<head>
<script type="text/javascript">
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","postback.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<form>
<!--<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>-->
<input type="text" name="users" onchange="showUser(this.value)">
</form>
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>

</body>
</html>

postback.php

<?php
$q=$_GET['q'];

$con = mysql_connect('localhost', 'root', '');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("ajax_demo", $con)or die('cannot connect');

$sql = 'SELECT * FROM `ajax_demo_table`  WHERE `FirstName` LIKE CONVERT(_utf8 \'%'.$q.'%\' USING latin1) COLLATE latin1_swedish_ci OR `LastName` LIKE CONVERT(_utf8 \'%'.$q.'%\' USING latin1) COLLATE latin1_swedish_ci'; 

$result = mysql_query($sql);//or die('cannot query '.$result);

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "<td>" . $row['Age'] . "</td>";
  echo "<td>" . $row['Hometown'] . "</td>";
  echo "<td>" . $row['Job'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>

I have a question on this right now. I made this on my own on. If you read between the lines.., I made it similar to auto post back of ASP.net what I want to know now is this... for example I have stored some data in my database let's say Peter Griffin, Lois Griffin, John Wycliffe and Elton John. According in to the logical program of my PHP... the query in my PHP is a wild card select of First Name and Last Name. Now the User input a name.. let's say Elton Wycliffe... It will Release. John Wycliffe and Elton John... My problem here right now is only the First word is accepted to search it... I dunno what could I do with the proceeding words that will also search it... Like this way... Elton is the first word the user has inputted and Wycliffe is the second word. I want every words are being searched in the database so that it will release the suggested outcome. that's what i want to have it right now...( thanks for reading my post... )

Recommended Answers

All 8 Replies

Try this code.

$q = preg_replace('/\s\s+/', ' ', $q); // remove extra spaces
	$sql = "SELECT *,CONCAT(FirstName, ' ',LastName) as fullname WHERE CONCAT(FirstName, ' ',LastName) LIKE  CONVERT(_utf8 '%".$q."%' USING latin1) COLLATE latin1_swedish_ci ";

Try this code.

$q = preg_replace('/\s\s+/', ' ', $q); // remove extra spaces
	$sql = "SELECT *,CONCAT(FirstName, ' ',LastName) as fullname WHERE CONCAT(FirstName, ' ',LastName) LIKE  CONVERT(_utf8 '%".$q."%' USING latin1) COLLATE latin1_swedish_ci ";

should I replace the line number 2 in php with the code you posted above???

No. Just add my code at line number 12 and remove your $sql query.

Try this code.

$q = preg_replace('/\s\s+/', ' ', $q); // remove extra spaces
	$sql = "SELECT *,CONCAT(FirstName, ' ',LastName) as fullname WHERE CONCAT(FirstName, ' ',LastName) LIKE  CONVERT(_utf8 '%".$q."%' USING latin1) COLLATE latin1_swedish_ci ";

Itried what yo had said but it releases an error... I tired to comment out the line I done in line 2 and use the code you had done... and I tried the $sql you maid... but it releases an error... but i think there's something suspicious... is there realy SELECT * with no From then a " ,CONCAT" in MySQL syntax?

Itried what yo had said but it releases an error... I tired to comment out the line I done in line 2 and use the code you had done... and I tried the $sql you maid... but it releases an error... but i think there's something suspicious... is there realy SELECT * with no From then a " ,CONCAT" in MySQL syntax?

something realy is suspicious in $sql u modified.... aside from that.. it will also relase the data information of that person... describing only address and the career of the person.

Oh i forget FROM.
Try this code and post output.

<?php
$q=$_GET['q'];

$con = mysql_connect('localhost', 'root', '');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("ajax_demo", $con)or die('cannot connect');

$q = preg_replace('/\s\s+/', ' ', $q); // remove extra spaces
$sql = "SELECT *,CONCAT(FirstName, ' ',LastName) as fullname FROM `ajax_demo_table` WHERE CONCAT(FirstName, ' ',LastName) LIKE  CONVERT(_utf8 '%".$q."%' USING latin1) COLLATE latin1_swedish_ci ";

$result = mysql_query($sql);//or die('cannot query '.$result);

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "<td>" . $row['Age'] . "</td>";
  echo "<td>" . $row['Hometown'] . "</td>";
  echo "<td>" . $row['Job'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>

Try this code.

$q = preg_replace('/\s\s+/', ' ', $q); // remove extra spaces
	$sql = "SELECT *,CONCAT(FirstName, ' ',LastName) as fullname WHERE CONCAT(FirstName, ' ',LastName) LIKE  CONVERT(_utf8 '%".$q."%' USING latin1) COLLATE latin1_swedish_ci ";

I tried it... it was now on the way. thanks to the earlier code you had posted. now this is my question...

the scenario right now is like a google search engine (or any search engine) what I want right now is if a user inputted a wrong statement to be searched but there are possible results like "Elton Wycliffe" is inputted by the user and there's no result but there are possible outcome "Elton John" and "John Wycliffe"... which is in a link like in google's possible result statement "do you mean you want to search Elton John" in a hyper link then once it is clicked it will release the right outcome due to the server corrections

I tried it... it was now on the way. thanks to the earlier code you had posted. now this is my question...

the scenario right now is like a google search engine (or any search engine) what I want right now is if a user inputted a wrong statement to be searched but there are possible results like "Elton Wycliffe" is inputted by the user and there's no result but there are possible outcome "Elton John" and "John Wycliffe"... which is in a link like in google's possible result statement "do you mean you want to search Elton John" in a hyper link then once it is clicked it will release the right outcome due to the server corrections

I quoted my own reply right now... I will post htis one for a purpose. I already done this problem thanks to the explode function

<?php
$q=$_GET['q'];
$words=explode(' ',$q);
//for( $i = 0; $i < count($words); ++$i )
//echo $i.'. '.$words[$i].'
//';
$con = mysql_connect('localhost', 'root', '');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("ajax_demo", $con)or die('cannot connect');

$q = preg_replace('/\s\s+/', ' ', $q); // remove extra spaces
$count=count($words);




//if the whole string is not compatible
for( $i = 0; $i < count($words); ++$i ){
echo $i.'. '.$words[$i].'
';
$sql = "SELECT *,CONCAT(FirstName, ' ',LastName) as fullname FROM `ajax_demo_table` WHERE CONCAT(FirstName, ' ',LastName) LIKE  CONVERT(_utf8 '%".$words[$i]."%' USING latin1) COLLATE latin1_swedish_ci ";

$result = mysql_query($sql)or die('cannot query '.$result);
/*echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
*/
echo "<table border='1'>";
while($row = mysql_fetch_array($result))
  { 
if($result >0){
echo "
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
 echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "<td>" . $row['Age'] . "</td>";
  echo "<td>" . $row['Hometown'] . "</td>";
  echo "<td>" . $row['Job'] . "</td>";
  echo "</tr>";
}else{
echo"<table border='1'><tr><td>do code here</td></tr></table>";
}
  }
echo "</table>";
}

mysql_close($con);
?>

this is the revised solved code now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.