thenetduck
October 29th, 2007, 08:22 AM
HI
I am learning PHP / MySQL with a Wrox book (good stuff) and need some help understand a part of it.
In the book you create a fake movie review website. I am at the point where we created a database and I am now calling values from it to appear on the website. I don't understand how the while and the foreach works.
<?php
// connect to MySQL
$connect = mysql_connect("localhost", "username","password") or die ("yo ***** roche you didn't connect!");
// make sure I am selecting the right db
mysql_select_db("moviesite");
$query = "SELECT movie_name, movie_type " .
"FROM movie " .
"WHERE movie_year>1990 " .
"ORDER BY movie_type";
$results = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_assoc($results))
{
foreach($row as $val1)
{
echo $val1;
echo " ";
}
echo "<br />";
}
?>
as you can see there is some $val1 variable and it doesn't explain how assoc works etc. I can program in c++ so am not unfamiliar with whiles and for loops but this one has me scratching my head.
One more question while I'm here. When values are stored in results how are they stored? in an array? If so where does that array start with the table? Wrox is good, but doesn't go into any detail to enlighten me on this subject.
Thank you everyone!
The Net Duck
I am learning PHP / MySQL with a Wrox book (good stuff) and need some help understand a part of it.
In the book you create a fake movie review website. I am at the point where we created a database and I am now calling values from it to appear on the website. I don't understand how the while and the foreach works.
<?php
// connect to MySQL
$connect = mysql_connect("localhost", "username","password") or die ("yo ***** roche you didn't connect!");
// make sure I am selecting the right db
mysql_select_db("moviesite");
$query = "SELECT movie_name, movie_type " .
"FROM movie " .
"WHERE movie_year>1990 " .
"ORDER BY movie_type";
$results = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_assoc($results))
{
foreach($row as $val1)
{
echo $val1;
echo " ";
}
echo "<br />";
}
?>
as you can see there is some $val1 variable and it doesn't explain how assoc works etc. I can program in c++ so am not unfamiliar with whiles and for loops but this one has me scratching my head.
One more question while I'm here. When values are stored in results how are they stored? in an array? If so where does that array start with the table? Wrox is good, but doesn't go into any detail to enlighten me on this subject.
Thank you everyone!
The Net Duck