[PHP-DB] search result error message
Hi, The below mentioned code works fine. Connects to the database, fetches the result, and displays neatly in a table. If there is no data then it jumps to the if condition and displays the error message. BUT if the 'if' condition is running and no data is present the Table headings and the bottm of empty table is still displayed above the error message. Can you please check and share with me as in where exactly the logic is wrong. -- -- Code --- h2Your favorites search result/h2divYou are here: index.htm Home Your favorites/div div class=internalContentArea sgtable div class=tableHeadSub-category search result/div div table width='100%' id='table1' cellspacing=0theadtr th scope=colShop name/th /tr/theadtbody ?php // database information $host = 'xxx'; $user = 'xxx'; $password = 'xxx'; $dbName = 'xxx'; mysql_connect(localhost,$user,$password); @mysql_select_db($dbName ) or die( Sorry But There Seems To Be A Problem Connecting To The Database); $query=SELECT * FROM shop; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); $i=0; while ($i $num) { $shopname=mysql_result($result,$i,shopname); echo tr td$shopname/td /tr; $i++; } ?/tbody/table index.htm Back to search div /div ? if ($num == 0){ echo div div class='alert'The search criteria you entered did not generate any result, index.htm please try again ./div /div; }? /div. -- View this message in context: http://www.nabble.com/search-result-error-message-tf2867391.html#a8014018 Sent from the Php - Database mailing list archive at Nabble.com. -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] search result error message
Sorry, don't have time to test and noodle through why yours may or may not be working, but I see some differences in how you're doing it and how we tend to do it here. After doing the connection and database selection, this is how we handle stuff (simplified): $query = select * from sometable; $result = mysql_query($query); $count = mysql_affected_rows(); while ($row = mysql_fetch_assoc($result)) { // do some stuff $address = $row['address']; // example of how to get/use data returned } Nothing inside the while construct should execute if you get zero results. You really don't need to use the mysql_affected_rows() either unless you want to test for zero results and display an error message, like I believe you wanted to do. -TG = = = Original message = = = Hi, The below mentioned code works fine. Connects to the database, fetches the result, and displays neatly in a table. If there is no data then it jumps to the if condition and displays the error message. BUT if the 'if' condition is running and no data is present the Table headings and the bottm of empty table is still displayed above the error message. Can you please check and share with me as in where exactly the logic is wrong. -- -- Code --- h2Your favorites search result/h2divYou are here: index.htm Home Your favorites/div~~ div class=internalContentArea sgtable ~~~div class=tableHeadSub-category search result/div~~ ~div ~table width='100%' id='table1' cellspacing=0theadtr th scope=colShop name/th /tr/theadtbody ?php // database information $host = 'xxx'; $user = 'xxx'; $password = 'xxx'; $dbName = 'xxx'; mysql_connect(localhost,$user,$password); @mysql_select_db($dbName ) or die( Sorry But There Seems To Be A Problem Connecting To The Database); $query=SELECT * FROM shop; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); $i=0; while ($i $num) $shopname=mysql_result($result,$i,shopname); echo tr td$shopname/td /tr; $i++; ?/tbody/table index.htm Back to search div /div ? if ($num == 0) echo div div class='alert'The search criteria you entered did not generate any result, index.htm please try again ./div ~/div; ? ~ /div. -- View this message in context: http://www.nabble.com/search-result-error-message-tf2867391.html#a8014018 Sent from the Php - Database mailing list archive at Nabble.com. ___ Sent by ePrompter, the premier email notification software. Free download at http://www.ePrompter.com. -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] search result error message
Hi, there's no 'logic' error at all that I can see. It's more a design error as it doesn't do the job the way that you describe it at all. What actually happens is that the starting HTML (heading and table start) is output before the database is even contacted, so there's no way for you to take the table back if you don't receive any relevant hits. try something more like this: ?php // database information $host = 'xxx'; $user = 'xxx'; $password = 'xxx'; $dbName = 'xxx'; mysql_connect(localhost,$user,$password); @mysql_select_db($dbName ) or die( Sorry But There Seems To Be A Problem Connecting To The Database); $query=SELECT * FROM shop; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); if ($num) { ? h2Your favorites search result/h2divYou are here: index.htm Home Your favorites/div div class=internalContentArea sgtable div class=tableHeadSub-category search result/div div table width='100%' id='table1' cellspacing=0theadtr th scope=colShop name/th /tr/theadtbody ?php $i=0; while ($i $num) { $shopname=mysql_result($result,$i,shopname); echo tr td$shopname/td /tr; $i++; } ?/tbody/table index.htm Back to search div /div } else { echo div div class='alert'The search criteria you entered did not generate any result, index.htm please try again ./div /div; } You'll probably need to polish this some, because I quickly cut/pasted it before I go down the pub. Niel -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php