kelseyirene Posted December 11, 2009 Share Posted December 11, 2009 Hi! I have a question about my coding for this. I have two separate files. The first is a search from where I search an author's last name, and the second is a php file that outputs the author first and last name, photo, and other titles by that are all stored in my database. The code seems to mostly work fine except for one problem: it outputs the number of times equal with the number of titles stores under the author's name. So, for Roberto Bolano, it outputs five times because there are five titles. Is there anyway I can make the code repeat the query only once? <? include("connect.php"); $search=$_POST['search']; $query = mysql_query("SELECT * FROM TitleInfo WHERE author_last LIKE '%$search%' LIMIT 0, 50") or trigger_error (mysql_error()); while ($row = @mysql_fetch_array($query)) { $var3=$row["author_first"]; $var4=$row["author_last"]; echo "<h1>"; echo $var3; echo " "; echo $var4; echo "</h1>"; echo "<br><br>"; echo "<img src='images/$search.jpg' border=1>"; echo "<br><br>"; echo "Other titles by:<br>"; $sql = mysql_query("SELECT * FROM TitleInfo WHERE author_last LIKE '%$search%'") or trigger_error (mysql_error()); while($r=mysql_fetch_array($sql)) { $v1=$r["title"]; echo $v1; echo "<br>"; } } ?> thank you so so much! Quote Link to comment https://forums.phpfreaks.com/topic/184815-help-with-my-coding-for-search-output/ Share on other sites More sharing options...
ignace Posted December 11, 2009 Share Posted December 11, 2009 Yes. Don't use a while. Quote Link to comment https://forums.phpfreaks.com/topic/184815-help-with-my-coding-for-search-output/#findComment-975682 Share on other sites More sharing options...
dawsba Posted December 12, 2009 Share Posted December 12, 2009 have a look into GROUP BY in your main sql statement Quote Link to comment https://forums.phpfreaks.com/topic/184815-help-with-my-coding-for-search-output/#findComment-975811 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.