IwnfuM Posted September 7, 2010 Share Posted September 7, 2010 hi , i have build an search engine . i don't know really how to output the results. i have done something like this . $qry = mysql_fetch_assoc(mysql_query("SELECT `search2`FROM `search` WHERE `search1`='%".$search."%' ")); echo $qry['search2']; but it output only 1 result , i wanna know to to output all the results he find. thanks , Mor. Quote Link to comment https://forums.phpfreaks.com/topic/212791-loop-input/ Share on other sites More sharing options...
coupe-r Posted September 7, 2010 Share Posted September 7, 2010 Do this: $result = mysql_query("SELECT search2 FROM search`WHERE search1 = '%".$search."%' "); $row = mysql_fetch_array($result); echo $row['search2']; Quote Link to comment https://forums.phpfreaks.com/topic/212791-loop-input/#findComment-1108399 Share on other sites More sharing options...
Psycho Posted September 7, 2010 Share Posted September 7, 2010 $query = "SELECT `search2`FROM `search` WHERE `search1`='%{$search}%'"; $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { //Include code to output results for each record } Quote Link to comment https://forums.phpfreaks.com/topic/212791-loop-input/#findComment-1108400 Share on other sites More sharing options...
coupe-r Posted September 7, 2010 Share Posted September 7, 2010 Yes, what he said. I forgot you wanted multiple rows. Quote Link to comment https://forums.phpfreaks.com/topic/212791-loop-input/#findComment-1108402 Share on other sites More sharing options...
rwwd Posted September 7, 2010 Share Posted September 7, 2010 You need to loop through the results if you know that there is going to be more than one result. Ie:- //I find it handy to build the sql outside the function, this makes debugging easier! $searchTerms = "SELECT `search2`FROM `search` WHERE `search1`='%".$search."%' "; $sqlSearch = mysql_query($searchTerms); //start loop here while($qry = mysql_fetch_assoc($sqlSearch)){ //you could do this as a method of debugging also:- //echo "<pre>"; //print_r($qry); //echo "</pre>"; //print the results to screen echo $qry['search2']."<br>"; } //close the loop Hope that helps a little anyway, Cheers, Rw I wish I could type faster on some days!! What they said anyway!! Quote Link to comment https://forums.phpfreaks.com/topic/212791-loop-input/#findComment-1108403 Share on other sites More sharing options...
IwnfuM Posted September 7, 2010 Author Share Posted September 7, 2010 mjdamato , you write it like that : '%{$search}%' and I wrote it like that : '%".$search."%' is there a difference ? and you'r code will print all the results that have founded? how can i know how many have been found ? thanks , Mor. Quote Link to comment https://forums.phpfreaks.com/topic/212791-loop-input/#findComment-1108405 Share on other sites More sharing options...
Psycho Posted September 7, 2010 Share Posted September 7, 2010 mjdamato , you write it like that : '%{$search}%' and I wrote it like that : '%".$search."%' is there a difference ? No difference. I don't like the look of code that concatenates variables how you had it. Besides, when using double quotes, the PHP parser will interpret variables in the string. If you are going to exit out of the string to concatenate a variable, why use double quotes. You can just put the variable inside the double quotes, but enclosing them in curly braces helps alleviate certain problems - such as using arrays. and you'r code will print all the results that have founded? how can i know how many have been found ? The code I posted create a loop where YOU can insert the code for displaying the results. I don't know how you want to display them so I left it out. But, here is one example where each result is displayed on a separate line. I also added code to display the number of records returned. $query = "SELECT `search2`FROM `search` WHERE `search1`='%{$search}%'"; $result = mysql_query($query); if(mysql_num_rows($result)==0) { echo "There were no results."; } else { echo "There were " . mysql_num_rows($result) . " records found:<br />\n"; while($row = mysql_fetch_assoc($result)) { echo " - {$qry['search2']}<br />\n";; } } Quote Link to comment https://forums.phpfreaks.com/topic/212791-loop-input/#findComment-1108417 Share on other sites More sharing options...
IwnfuM Posted September 7, 2010 Author Share Posted September 7, 2010 helped me lot ! thanks , Mor. Quote Link to comment https://forums.phpfreaks.com/topic/212791-loop-input/#findComment-1108421 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.