Lambneck Posted August 17, 2009 Share Posted August 17, 2009 Hello, I am trying to select multiple rows from a table based on the the id passed with GET. The following doesnt display anything. Just blank space. $id = (int) $_GET['id']; $sql = "SELECT * FROM $table WHERE post_id=$id"; $result = mysql_query($sql) or die("Error ". mysql_error(). " with query ". $sql); if(mysql_num_rows($result) == 1){ $row = mysql_fetch_array($result); echo 'Tags: '; $tags = $row['tag']; $tags_array = explode(",", $tags); foreach($tags_array as $value) { $value = trim($value); echo '<a href="tags.php?id='.urlencode($value).'" title="view posts tagged '.$value.'">'.$value ."</a> "; } } Link to comment https://forums.phpfreaks.com/topic/170657-solved-selecting-multiple-rows/ Share on other sites More sharing options...
Mark Baker Posted August 17, 2009 Share Posted August 17, 2009 This line if(mysql_num_rows($result) == 1){ is telling php to display only if there is a single result returned by the query. If you're expecting multipl results, you should change it to if(mysql_num_rows($result) >= 1){ You'll also need to loop through the result set with a while loop: while ($row = mysql_fetch_array($result)) { Link to comment https://forums.phpfreaks.com/topic/170657-solved-selecting-multiple-rows/#findComment-900094 Share on other sites More sharing options...
Lambneck Posted August 17, 2009 Author Share Posted August 17, 2009 Ok this helps! but its still not displaying all the relevant tags. looks like just two tags are displayed even though there should be more. $id = (int) $_GET['id']; echo 'Tags: '; $sql = "SELECT * FROM $table WHERE post_id=$id"; $result = mysql_query($sql) or die("Error ". mysql_error(). " with query ". $sql); if(mysql_num_rows($result) >= 1){ $row = mysql_fetch_array($result); while ($row = mysql_fetch_array($result)) { $tags = $row['tag']; $tags_array = explode(",", $tags); foreach($tags_array as $value) { $value = trim($value); echo '<a href="tags.php?id='.urlencode($value).'" title="view posts tagged '.$value.'">'.$value ."</a> "; } } } Link to comment https://forums.phpfreaks.com/topic/170657-solved-selecting-multiple-rows/#findComment-900115 Share on other sites More sharing options...
Lambneck Posted August 18, 2009 Author Share Posted August 18, 2009 bump Link to comment https://forums.phpfreaks.com/topic/170657-solved-selecting-multiple-rows/#findComment-900925 Share on other sites More sharing options...
PFMaBiSmAd Posted August 18, 2009 Share Posted August 18, 2009 The following line of code, right before the while() loop, is fetching and discarding the first row from the result set - $row = mysql_fetch_array($result); Why is that line of code in your program? Link to comment https://forums.phpfreaks.com/topic/170657-solved-selecting-multiple-rows/#findComment-900927 Share on other sites More sharing options...
Lambneck Posted August 18, 2009 Author Share Posted August 18, 2009 Thank you sir. Link to comment https://forums.phpfreaks.com/topic/170657-solved-selecting-multiple-rows/#findComment-900971 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.