So, I’m building my first super basic CMS using PHP. I don’t want to simply copy the code from the tutorial I’m watching, but really understand it. One thing that bothers me is the use of the while loop for fetching posts. In the code below, I can’t see how the statement inside within the rounded brackets constitute a condition. Seems to me all it does is assigning an array to the variable $post. How can you loop over something that is not a condition is my question, I suppose. Thanks!
function get_posts () {
$query = mysql_query("SELECT * FROM posts") or die(mysql_error());
while ($post = mysql_fetch_assoc($query)) {
echo $post['Content'];
}
}
STOP!
The tutorial you’re watching is severely outdated! Please, don’t use
mysql_*functions in new code. They are no longer maintained and the deprecation process has begun on it. See the red box? Learn about prepared statements instead, and use PDO or MySQLi – this article will help you decide which. If you choose PDO, here is a good tutorial.As for the specific question, this line
Is the condition.
mysql_fetch_assoc()returns an array if there are more results, andfalse, in the case there aren’t any. If it returnsfalse, the condition will evaluate tofalseand the loop would break.