Jump to content

[SOLVED] selecting multiple rows


Lambneck

Recommended Posts

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.  :facewall:

 

 

$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

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)) {

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> ";
	}
}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.