Hi, I have made a script to search my database. But when I execute it, it shows only one result. I even checked the database but there were many entries like this.

here's the code...

<?php 

// Made by Shahbaz Singh July 2011. All Rights Reserved. Webdhaba.in

$q = $_GET["q"];

$db_host = "localhost";
// Place the username for the MySQL database here
$db_username = ""; 
// Place the password for the MySQL database here
$db_pass = ""; 
// Place the name for the MySQL database here
$db_name = "test";

// Run the connection here 
mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysql_select_db("$db_name") or die ("no database");
$sql = mysql_query("SELECT * FROM people WHERE name LIKE '%$q%'");

$num_results = 0;

while($row = mysql_fetch_array($sql))
{
$num_results++;
$name = $row["name"];

$id = $row["id"];

$default_pic = "people/$id/image01.jpg";

$no_pic = "people/0/no_image_pic.jpg";

if (file_exists($default_pic)) {

$image = "$default_pic"; } else {
$image = "$no_pic"; 

}

}

if($num_results == 0)
{
$output = "<table border='0' width='100%' cellspacing='0' cellpadding='0' >
	<tr>
		<td colspan='2' height='24' >
		<p align='center'><b><font face='Arial' size='2'>Search Results</font></b></td>
	</tr>
	<tr>
		<td align='center' width='100%' >
		<font face='Arial' size='2'><br><br><br>Sorry, no results could be found.
		</font></b></td>
		</tr>
</table>";
} else {

$output = "<table border='0' width='100%' cellspacing='0' cellpadding='0' >
	<tr>
		<td colspan='2' height='24' >
		<p align='center'><b><font face='Arial' size='2'>Search Results</font></b></td>
	</tr>
	<tr>
		<td width='67' >
		<b><font face='Arial' size='2'>
		<img border='0' src='$image' width='86' height='86'></font></b></td>
		<td width='967' >
		<b><font face='Arial' size='2'>&nbsp;&nbsp;&nbsp; 
		<a href='people.php?id=$id'>
		<span style='text-decoration: none'>$name
		</span></a></font></b></td>
	</tr>
</table>";
}
?>

You should use array to get the data withing the loop. If not, the second will override the first and the third will override the second and so on. Finally, you would have only result, the last one. Check Line 25,

$name = $row["name"];

Should be

$name[] = $row["name"];

All names will store in $name array match with your search keyword. If you have another variable to store like this, change it to array too.

You should use array to get the data withing the loop. If not, the second will override the first and the third will override the second and so on. Finally, you would have only result, the last one. Check Line 25,
php Syntax (Toggle Plain Text)
$name = $row["name"];
Should be
php Syntax (Toggle Plain Text)
$name[] = $row["name"];

Hey Zero13

Now it does not show the name, it shows "ARRAY"...

Please Help

Now, the name array has all matches with the keyword that the user search. Echo out the name with for loop.

for($i=0;$i<count($name);$i++){
      echo $name[$i] . '<br />';
}

You can also use 'foreach' method.

foreach($name as $user) {
       echo $user . '<br />';
}

Hope this help.

Please can you apply this code on my script...please.....

Thanks a lot...

Plzzzzzzzzz help...!!!

Try this.

<?php 

// Made by Shahbaz Singh July 2011. All Rights Reserved. Webdhaba.in

$q = $_GET["q"];

$db_host = "localhost";
// Place the username for the MySQL database here
$db_username = ""; 
// Place the password for the MySQL database here
$db_pass = ""; 
// Place the name for the MySQL database here
$db_name = "test";

// Run the connection here 
mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysql_select_db("$db_name") or die ("no database");
$sql = mysql_query("SELECT * FROM people WHERE name LIKE '%$q%'");

$num_results = mysql_num_rows($sql);
if($num_results > 0){
	$count=0;
	while($row = mysql_fetch_array($sql)){
		
		$users[$count]->name = $row["name"];

		$users[$count]->id = $row["id"];

		$default_pic = "people/$id/image01.jpg";

		$no_pic = "people/0/no_image_pic.jpg";

		if (file_exists($default_pic)) {
			$image = "$default_pic"; 
		} 
		else {
			$image = "no_pic";
		}
		$users[$count]->avatar = $image;
		
		$count++;
	}
	
	$output = "<table border='0' width='100%' cellspacing='0' cellpadding='0' >
	<tr>
		<td colspan='2' height='24' >
		<p align='center'><b><font face='Arial' size='2'>Search Results</font></b></td>
	</tr>";
	
	foreach($users as $user){
	
		$output .= "<tr>
			<td width='67' >
			<b><font face='Arial' size='2'>
			<img border='0' src='$user->avatar' width='86' height='86'></font></b></td>
			<td width='967' >
			<b><font face='Arial' size='2'>&nbsp;&nbsp;&nbsp; 
			<a href='people.php?id=$user->id"'>
			<span style='text-decoration: none'>$user->name
			</span></a></font></b></td>
			</tr>";
	}
}
else {
	$output = "<table border='0' width='100%' cellspacing='0' cellpadding='0' >
	<tr>
		<td colspan='2' height='24' >
		<p align='center'><b><font face='Arial' size='2'>Search Results</font></b></td>
	</tr>
	<tr>
		<td align='center' width='100%' >
		<font face='Arial' size='2'><br><br><br>Sorry, no results could be found.
		</font></b></td>
		</tr>";
}
$output .= "</table>";
?>
?>

Not tested yet. You may encounter the errors. If you got something wrong. Post here.

Hope this help.

Thankyou Zero13.... Bestest Post Ever ... It worked easily but there were a few mistakes but worked very well...

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.