Jump to content

[SOLVED] Mysql Loop Unknown Error.


lszanto

Recommended Posts

I'm working on a simple script at the moment and it requires to sort different data into sections and these sections are chosen by there 'list' title, for some reason the sql only loops once so I need to know why its not looping more, if you need more info please ask and thanks in advance.

 

		<?php

	//Make query.
	$sql = "SELECT * FROM lists";

	//Make query into result.
	$result = mysql_query($sql);

	//Run query loop.
	while($row = mysql_fetch_array($result)) {
		//Get list.
		$list = $row['title'];

		//Show list.
		echo "<br /><h3>$list</h3>";

		//Make new query.
		$sql = "SELECT title, description, price FROM wish_list WHERE list='$list'";

		//Make result.
		$result = mysql_query($sql);

		//Run result.
		while($data = mysql_fetch_array($result)) {
			//Show each piece of data.
			echo "<strong>Title:</strong>" . $data['title'] . "<br />";
			echo "<strong>Description:</strong>" . $data['description'] . "<br />";
			echo "<strong>Price:</strong> $" . $data['price'] . "<br /><br />";
		}

		//Break lines.
		echo "<br /><br />";
	}

	?>

Link to comment
https://forums.phpfreaks.com/topic/70877-solved-mysql-loop-unknown-error/
Share on other sites

Yours looks fine, but give this a go.

 

<?php

$sql = "SELECT * FROM `lists";
$res = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_assoc($res)){
  $list = $row['title'];
  echo "<br /><h3>$list</h3>\n";
  
    $sql2 = "SELECT * FROM `wish_list` WHERE `list`='$list'";
    $res2 = mysql_query($sql2) or die(mysql_error());
    
    while($row2 = mysql_fetch_assoc($res2)){
    echo "<strong>Title:</strong> ". $row2['title'] ." <br />\n";
    echo "<strong>Description:</strong> ". $row2['description'] ."<br />\n";
    echo "<strong>Price:</strong> ". $row['price'] ."<br />\n";
    }
  echo "<br /><br />\n";
}

?>

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.