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

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.