ballouta Posted April 5, 2010 Share Posted April 5, 2010 Hello I am looping over several tables in my DB to search fro certain keyword, I want to display a simple horizonal line after each table search result <hr size='1'> if some tables doesnt return any result, it will display the horizonal line so i get liek duplicated lines with empty space between them. my code was like this: $result = mysql_query("SELECT * FROM `links_en` where `lnk` LIKE '%$kw%' OR `orga` LIKE '%$kw%'"); echo "<hr size='1'><ul dir='ltr'> "; while ($row = mysql_fetch_array($result)) { //view lines go here } So I added this condition to view the line (seprartor): $result = mysql_query("SELECT * FROM `phdp_en` where `title` LIKE '%$kw%' OR `body` LIKE '%$kw%'"); $row = mysql_fetch_array($result); if (mysql_num_rows($result) > 0) { echo "<hr size='1'>"; // ballouta comment } else {} echo "<ul dir='ltr'>"; while ($row = mysql_fetch_array($result)) { //view lines go here } The problem is that after checking the rows number (in the above condition) i am loosing the query, so I had to include it in the (ballouta comment) but i think this is very silly and makes the query runs twice, what is the correct logic and solutions? Many thanks Link to comment https://forums.phpfreaks.com/topic/197590-search-help/ Share on other sites More sharing options...
Psycho Posted April 5, 2010 Share Posted April 5, 2010 If you are not using ALL of the columns for the table, then specify the columns you will be using in the SELECT statement instead of using *. Using * is inefficient and is bad practice in many cases. $query = "SELECT * FROM `phdp_en` WHERE `title` LIKE '%$kw%' OR `body` LIKE '%$kw%'"; $result = mysql_query($query); if (mysql_num_rows($result) > 0) { //There were results echo "<ul dir=\"ltr\">\n"; while ($row = mysql_fetch_array($result)) { //view lines go here } echo "</ul>\n"; } Link to comment https://forums.phpfreaks.com/topic/197590-search-help/#findComment-1036995 Share on other sites More sharing options...
andrewgauger Posted April 5, 2010 Share Posted April 5, 2010 I took a line out, try this: $result = mysql_query("SELECT * FROM `phdp_en` where `title` LIKE '%$kw%' OR `body` LIKE '%$kw%'"); //took this line out if (mysql_num_rows($result) > 0) { echo "<hr size='1'>"; // ballouta comment } else {} echo "<ul dir='ltr'>"; while ($row = mysql_fetch_array($result)) { //view lines go here } Link to comment https://forums.phpfreaks.com/topic/197590-search-help/#findComment-1037002 Share on other sites More sharing options...
ballouta Posted April 5, 2010 Author Share Posted April 5, 2010 Thank You mjdamato & andrewgauger it is working now and I appreciate your help. Link to comment https://forums.phpfreaks.com/topic/197590-search-help/#findComment-1037027 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.