sheraz Posted January 19, 2009 Share Posted January 19, 2009 sir, i wanto to search data in the mysql database using php.i want to output like this.. if(data exists) then print search results are then print table else print "sorry 0 search results "; plz help me how i can do this Quote Link to comment https://forums.phpfreaks.com/topic/141416-search-in-database-using-php/ Share on other sites More sharing options...
lostprophetpunk Posted January 19, 2009 Share Posted January 19, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/141416-search-in-database-using-php/#findComment-740271 Share on other sites More sharing options...
sheraz Posted January 30, 2009 Author Share Posted January 30, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/141416-search-in-database-using-php/#findComment-750416 Share on other sites More sharing options...
Philip Posted January 30, 2009 Share Posted January 30, 2009 Are you wanting to search the whole database, and not just one table? Quote Link to comment https://forums.phpfreaks.com/topic/141416-search-in-database-using-php/#findComment-750427 Share on other sites More sharing options...
phpSensei Posted January 30, 2009 Share Posted January 30, 2009 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/141416-search-in-database-using-php/#findComment-750433 Share on other sites More sharing options...
landavia Posted January 30, 2009 Share Posted January 30, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/141416-search-in-database-using-php/#findComment-750458 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.