I don't have any syntax error with this code on my PHP file.. I have only a problem when it comes to output of the search query error messaging...

here's the code

postback.php

<?php
$q=$_GET['q'];
$words=explode(' ',$q);
$con = mysql_connect('localhost', 'root', '')or  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'; 
$q = preg_replace('/\s\s+/', ' ', $q); // remove extra space
for( $i = 0; $i < count($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'>";

//if(mysql_fetch_array($result)>=1){
echo"
<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>";
  
  }if(mysql_fetch_array($result)==0){
echo "<tr><td><b>NO<b> similar names with <font color=red><b>".$words[$i].'</b></font></td></tr>';

}

/*else{
echo "<tr><td><b>NO<b> similar names with <font color=red><b>".$words[$i].'</b></font></td></tr>';

}*/

}

//end of while
//}

//}
echo "</table>";
mysql_close($con);
?>

what i want to occur now is when a user inputs a string or character or a number wherein it doesn't exists in database... there will be an error message such as "input query not existing"

to test the file here's the 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" onkeyup="showUser(this.value)">
<input type="reset" value="Clear All">

<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>
</form>
</body>
</html>

upload sql script with definition and data of ajax_demo_table

upload sql script with definition and data of ajax_demo_table

here's the script of mySQL ajax_demo.... this is the whole database

-- phpMyAdmin SQL Dump
-- version 2.11.9.2
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jun 08, 2011 at 04:26 AM
-- Server version: 5.0.67
-- PHP Version: 5.2.6

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- Database: `ajax_demo`
--

-- --------------------------------------------------------

--
-- Table structure for table `ajax_demo_table`
--

CREATE TABLE IF NOT EXISTS `ajax_demo_table` (
  `Number` int(11) NOT NULL auto_increment,
  `FirstName` varchar(50) NOT NULL,
  `LastName` varchar(50) NOT NULL,
  `Age` int(4) unsigned NOT NULL,
  `Hometown` varchar(500) NOT NULL,
  `Job` varchar(50) NOT NULL,
  PRIMARY KEY  (`Number`),
  FULLTEXT KEY `LastName` (`LastName`),
  FULLTEXT KEY `LastName_2` (`LastName`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

--
-- Dumping data for table `ajax_demo_table`
--

INSERT INTO `ajax_demo_table` (`Number`, `FirstName`, `LastName`, `Age`, `Hometown`, `Job`) VALUES
(1, 'Peter', 'Griffin', 50, 'Manila', 'Janitor'),
(2, 'Lois', 'Griffin', 30, 'USA', 'Club Dancer'),
(3, 'Glenn', 'Quagmire', 26, 'Qatar', 'Oil Tank driver'),
(4, 'Joseph', 'Swanson', 20, 'France', 'Chef'),
(5, 'Arwin', 'Avergas', 20, 'Valenzuela', 'Professional Night Club Manager');

I think problem is due to onkeyup event, it is constantly changing query.

<input type="text" name="users" onkeyup="showUser(this.value)">

change onkeyup to onblur

<input type="text" name="users" onblur="showUser(this.value)">

onblur is not applicable the costumer wants an auto response like ASP.NET's auto post back like in the google search engine.

Then you script is working fine. Now I am not able to understand what more you are expecting from it.

I have separated table, and now using one found flag.

<?php 		



$q=$_GET['q'];
$words=explode(' ',$q);
$con = mysql_connect('localhost', 'root', 'jaishal1110')or  die('Could not connect: ' . mysql_error());

mysql_select_db("test", $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'; 
$q = preg_replace('/\s\s+/', ' ', $q); // remove extra space
for( $i = 0; $i < count($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 ";
//echo $sql;

$result = mysql_query($sql) or die('cannot query '.$result);
echo "<table border='1'>";

//if(mysql_fetch_array($result)>=1){
echo"
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
$found=false;
while($row = mysql_fetch_array($result))
  {
$found=true;
  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 "<tr><td colspan=5><hr></font></td></tr>";
  }if(!$found){
echo "<tr><td><b>NO<b> similar names with <font color=red><b>".$words[$i].'</b></font></td></tr>';

}
echo "</table><hr>";


}

//end of while
//}

//}
//echo "</table><hr>";
mysql_close($con);

?>

I have separated table, and now using one found flag.

<?php 		



$q=$_GET['q'];
$words=explode(' ',$q);
$con = mysql_connect('localhost', 'root', 'jaishal1110')or  die('Could not connect: ' . mysql_error());

mysql_select_db("test", $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'; 
$q = preg_replace('/\s\s+/', ' ', $q); // remove extra space
for( $i = 0; $i < count($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 ";
//echo $sql;

$result = mysql_query($sql) or die('cannot query '.$result);
echo "<table border='1'>";

//if(mysql_fetch_array($result)>=1){
echo"
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
$found=false;
while($row = mysql_fetch_array($result))
  {
$found=true;
  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 "<tr><td colspan=5><hr></font></td></tr>";
  }if(!$found){
echo "<tr><td><b>NO<b> similar names with <font color=red><b>".$words[$i].'</b></font></td></tr>';

}
echo "</table><hr>";


}

//end of while
//}

//}
//echo "</table><hr>";
mysql_close($con);

?>

thank you so much friend... can you tell me what does $found do (right on the process) like how does it run?? does it mean it needs Boolean values??? you mentioned the word "flag" what does that mean here in programming... I mean how was really important or does it make sense in programming?

generally we use term FLAG for boolean kind of variable. FLAG is not a part of any programming language. Its just something to understand things in better manner.

Here I have used boolean variable or flag named $FOUND. Before starting loop I set it to false. If it goes in loop I set it to true, means that at least one record found. If query cause no record, the flag will remain false and it will print that message.

You may also use mysql_num_rows(), if you do not want to use flag

I am not sure your problem is solved or not, if yes then mark it solved.

commented: thnaks this thread has been solved +3

generally we use term FLAG for boolean kind of variable. FLAG is not a part of any programming language. Its just something to understand things in better manner.

Here I have used boolean variable or flag named $FOUND. Before starting loop I set it to false. If it goes in loop I set it to true, means that at least one record found. If query cause no record, the flag will remain false and it will print that message.

You may also use mysql_num_rows(), if you do not want to use flag

I am not sure your problem is solved or not, if yes then mark it solved.

yeah effectively mysql_num_rows() is also an easier one but the client of this program for search query wants mysql_fetch_array function for readability purposes... that's according to the client's regulations and agreement though my client is not a programmer but that was the agreement in the contract wherein readability purposes is an agreement.

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.