Hi all,
I have a database which I would like to use to power an HTML list.
I'm struggling to get the loop to print out more than one line.

This is the script:

<?php
$query = "SELECT * FROM `table` ORDER BY `year` ASC";
$resultID = mysql_query($query, $conn) or die("Data not found.");
for($y = 1 ; $y < mysql_num_rows($resultID) ; $y++){
$row = mysql_fetch_assoc($resultID);
$xml_output2 = "<li id=\"";
$xml_output2 .= $row['year']."\">";
$xml_output2 .= "<img src=\"/images/".$y.".png\" width=\"256\" height=\"256\" />";
$xml_output2 .= "<h1>".$row['location']."</h1><p>".$row[…
//echo $xml_output2;
}
?>

Database connection is okay, and columns all match.
On viewing the source, one line seems to have have been pulled in correctly, but that's all.

Please help! Thanks

Recommended Answers

All 5 Replies

Update: Line 2 should read:

$xml_output2 .= "<h1>".$row['location']."</h1><p>".$row['event']."</p></li>";
Member Avatar for jmichae3

your count is wrong. for mysql_num_rows case where there is 1 row, this function returns 1. you have chosen in your for loop to count from 1 (which is proper) to < mysql_num_rows() which would be in this case something less than 1 think of it like one less. the loop will not even execute the body in this case, because 1<1 is false. for COUNTS of things, you should
start at 1 and use <=
OR
start at 0 and use <

most people typically do the following in place of a for like you have done (this gets all the rows and takes less code: it's an assignment within a conditional of a while loop, and it also tests for end of data):

while ($row = mysql_fetch_assoc($resultID)) {

Thanks JMichael,
Unfortunately, still not working, and instead for 2nd row of MySQL.

<div id="display">
    	<?php
			$query = "SELECT * FROM `table` ORDER BY `year` ASC";
			$resultID = mysql_query($query, $conn) or die("Data not found.");
		    while ($row = mysql_fetch_assoc($resultID)) {
				//$row = mysql_fetch_assoc($resultID);
				$xml_output1 = "<li><a href=\"#".$row['year']."\">".$row['year']."</a></li>";
			}
			
			$query2 = "SELECT * FROM `table`ORDER BY `year` ASC";
			$resultID2 = mysql_query($query2, $conn) or die("Data not found.");
			//count rows
			$num_rows = mysql_num_rows($resultID2);
			while($row = mysql_fetch_assoc($resultID2)){
				//$row = mysql_fetch_assoc($resultID2);
				$xml_output2 = "<li id=\"";
				$xml_output2 .= $row['year']."\">";
				$xml_output2 .= "<img src=\"images/".$row['id'].".png\" width=\"256\" height=\"256\" />";
				$xml_output2 .= "<h1>".$row['location']."</h1><p>".$row['event']."</p></li>";
			}

		?>

Any more ideas - greatly appreciated

<?php
//DB Info
// connect to db
mysql_connect("$host", "$username", "$password")or die("cannot connect server "); 
mysql_select_db("$db_name")or die("cannot select DB");

$result = mysql_query("SELECT * FROM table ORDER BY year") 
or die(mysql_error());  
while($row = mysql_fetch_array( $result )) {

$xml_output2 = "<li id=\"";
$xml_output2 .= $row['year']."\">";
$xml_output2 .= "<img src=\"/images/".$y.".png\" width=\"256\" height=\"256\" />";
$xml_output2 .= "<h1>".$row['location']."</h1><p>".$row[…
}
?>

Codewalkz, JMichae3, thanks. Your suggestion for using while sent me back to the books, and yes it works thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.