Reputation: 40717
I wrote this code:
<?php
mysql_connect("localhost", "root", "root") or
die("Could not connect: " . mysql_error());
mysql_select_db("internal");
$result = mysql_query("SELECT Title, Message FROM msg");
?>
<?php
while ($row = mysql_fetch_array($result, MYSQL_NUM)){
?>
<div>
<h3><a href="#"><?php printf("%s", $row[0]); ?></a></h3>
<div><?php printf("%s", $row[1]); ?></div>
</div>
<?php
mysql_free_result($result);
}
?>
The result I´m getting is the first row of the MySQL table (With the correct formatting) I include an Image just in case:
(This is in fact the first row of my MySQL DB and the only thing I see)
The code was looping until I had to add the html tags, what I mean is that if I just do:
<?php printf("%s", $row[0]); ?>
&
<?php printf("%s", $row[1]); ?>
It looped and brought all of the results.
Could this be a syntax error?
Upvotes: 0
Views: 226
Reputation: 1412
You are freeing result after the first loop. Here
mysql_free_result($result);
}
This should be
}
mysql_free_result($result);
Upvotes: 1
Reputation: 4043
mysql_free_result($result);
inside your while loop. This makes it so after the first iteration, it clears the results, making so no more can be grabbed.
Change your ending to
<?php
}
mysql_free_result($result);
?>
and it'll fix it
Upvotes: 5
Reputation: 405
Doesn't mysql_free_result clear the result set?
You have nothing left to loop over!
Upvotes: 0
Reputation: 798666
Your third-to-last line frees the result before you're done with it. Move it outside the loop.
Upvotes: 2