Jump to content

search... in database using php


sheraz

Recommended Posts

You could look into mysql_num_rows() to see if any data exists in the database.

 

$sql1 = "SELECT * FROM `table`";
$res1 = mysql_query($sql1) or die(mysql_error());
$num_rows = mysql_num_rows($res1);

if($num_rows == '') { //if number of rows is equal to nothing
echo "There is no data in the database.\n";
}else {
//echo your table data here
}

 

Is that what you wanted?

  • 2 weeks later...

this code only gives me total number of rows in the database table

But

i want to search database values and the output should be like

 

if(rows found) then

 

print search results are:

 

print the search result data table;

 

else >:(

 

print "sorry no results found";

 

please help me.

Elaborate a little more please

 

sorry if i got any errors below or I didnt understand your question, im a little rusty, been off and on php for months.

 

<?php
$title = "Moby Dick";
$sql = mysql_query("SELECT `title`,`author`,`book` FROM `library` WHERE `title` LIKE '$title%'");

if(mysql_num_rows($sql) > 0){

$book = mysql_fetch_array($sql);
echo $book['title'];

}else{

echo "Sorry, but there was no books found with the name ".$title;

}
?>

imho phpsensei and lostprophetpunk answer same like i want to say

but plz.. we don't understand what do you want!? please describe

 

1. what table do you want to search?

 

2. is this ALL table on 1 database or just 1 table?

 

3. in 1st u said Data exist (mean data inside table) but next you said rows (rows name maybe?).. where the correct?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.