pelehelp 03-23-2009, 08:58 AM Hi,
Is me again. I have a question that when I do the nested foreach loop in the coding, I get an error such as 'Invalid argument supplied for foreach', I knew this can be happened if I try to do a foreach on an variable that isn't an array. But how to solve it? I not really sure about it,can someone kindly give some suggestion? Thanks.....
<?php
//Connect to the MySQL server
$db_host = "localhost";
$db_username = "root";
$db_password = "fsktm";
$server = mysql_connect($db_host,$db_username,$db_password)or die("Could not connect!/n");
//Select a database that has created
$db_name = "cinema";
mysql_select_db($db_name)or die("Could not select the database $db_name!\n");
//Retrieve or select the data
$category = array('CM1','HR1','AM1','AV1','AC1','SF1','DM1','RM1');
foreach($category as $genres)
{
$retrieve = mysql_query("SELECT * FROM movie WHERE category_id = $genres");
foreach($retrieve as $result)
{
echo"<table border = 1 width = 100% height = 100% style = 'background-color:#F0F8FF;'>
<tr>
<th>Movie_ID</th>
<th>Movie_Name</th>
<th>Duration</th>
<th>Description</th>
<th>Category_ID</th>
</tr>";
//Display the data from the database in a table using aray
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo"</td><td>";
echo $row['movie_id'];
echo "</td><td>";
echo $row['movie_name'];
echo "</td><td>";
echo $row['duration'];
echo "</td><td>";
echo $row['description'];
echo "</td><td>";
echo $row['category_id'];
echo "</tr>";
}
echo "</table>";
}
}
mysql_close($server);
?>
abduraooft 03-23-2009, 09:06 AM Please wrap PHP codes using the tag (http://www.codingforums.com/showthread.php?t=68462). You may edit your above post. Check the manual for examples and related function to know about php-mysql DB operations. See http://php.net/mysql_fetch_array
pelehelp 03-23-2009, 09:18 AM Very sorry ya cos I dono that my code is quite messy and I dono that it has the way to wrap PHP codes using the tag. Thanks for telling me.
<?php
//Connect to the MySQL server
$db_host = "localhost";
$db_username = "root";
$db_password = "fsktm";
$server = mysql_connect($db_host,$db_username,$db_password)or die("Could not connect!/n");
//Select a database that has created
$db_name = "cinema";
mysql_select_db($db_name)or die("Could not select the database $db_name!\n");
//Retrieve or select the data
$category = array('CM1','HR1','AM1','AV1','AC1','SF1','DM1','RM1');
foreach($category as $genres)
{
$retrieve = mysql_query("SELECT * FROM movie WHERE category_id = $genres");
foreach($retrieve as $result)
{
echo"<table border = 1 width = 100% height = 100% style = 'background-color:#F0F8FF;'>
<tr>
<th>Movie_ID</th>
<th>Movie_Name</th>
<th>Duration</th>
<th>Description</th>
<th>Category_ID</th>
</tr>";
//Display the data from the database in a table using aray
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo"</td><td>";
echo $row['movie_id'];
echo "</td><td>";
echo $row['movie_name'];
echo "</td><td>";
echo $row['duration'];
echo "</td><td>";
echo $row['description'];
echo "</td><td>";
echo $row['category_id'];
echo "</tr>";
}
echo "</table>";
}
}
mysql_close($server);
?>
pelehelp 03-23-2009, 09:21 AM So now can you kindly give some guidelines on how to solve the problem with the error display as such 'Invalid argument supplied for foreach()'?
oesxyl 03-23-2009, 09:36 AM use [ php] and [ /php] like this, please:
Hi,
Is me again. I have a question that when I do the nested foreach loop in the coding, I get an error such as 'Invalid argument supplied for foreach', I knew this can be happened if I try to do a foreach on an variable that isn't an array. But how to solve it? I not really sure about it,can someone kindly give some suggestion? Thanks.....
<?php
//Connect to the MySQL server
$db_host = "localhost";
$db_username = "root";
$db_password = "fsktm";
$server = mysql_connect($db_host,$db_username,$db_password)or die("Could not connect!/n");
//Select a database that has created
$db_name = "cinema";
mysql_select_db($db_name)or die("Could not select the database $db_name!\n");
//Retrieve or select the data
$category = array('CM1','HR1','AM1','AV1','AC1','SF1','DM1','RM1');
foreach($category as $genres)
{
$retrieve = mysql_query("SELECT * FROM movie WHERE category_id = $genres");
foreach($retrieve as $result)
{
echo"<table border = 1 width = 100% height = 100% style = 'background-color:#F0F8FF;'>
<tr>
<th>Movie_ID</th>
<th>Movie_Name</th>
<th>Duration</th>
<th>Description</th>
<th>Category_ID</th>
</tr>";
//Display the data from the database in a table using aray
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo"</td><td>";
echo $row['movie_id'];
echo "</td><td>";
echo $row['movie_name'];
echo "</td><td>";
echo $row['duration'];
echo "</td><td>";
echo $row['description'];
echo "</td><td>";
echo $row['category_id'];
echo "</tr>";
}
echo "</table>";
}
}
mysql_close($server);
?>
you have problem with this foreach:
$retrieve = mysql_query("SELECT * FROM movie WHERE category_id = $genres");
foreach($retrieve as $result)
{
?>
$retrive is not a array, you don't need to use foreach here but you must check if is valid, so replace it with a if:
$retrieve = mysql_query("SELECT * FROM movie WHERE category_id = $genres");
if($retrieve){
echo"<table border = 1 width = 100% height = 100% style = 'background-color:#F0F8FF;'>
<tr>
<th>Movie_ID</th>
<th>Movie_Name</th>
<th>Duration</th>
<th>Description</th>
<th>Category_ID</th>
</tr>";
//Display the data from the database in a table using aray
while($row = mysql_fetch_array($retrieve))
best regards
pelehelp 03-23-2009, 10:30 AM Thanks for ur guide...It seems logically but now I cant even print out the output, so I wondering where is the error? Can you kindly suggest a way to solve it? I think that sth wrong with "$genres" in line $retrieve = mysql_query("SELECT * FROM movie WHERE category_id = $genres"); which it may not support string type.
oesxyl 03-23-2009, 10:40 AM Thanks for ur guide...It seems logically but now I cant even print out the output, so I wondering where is the error? Can you kindly suggest a way to solve it? I think that sth wrong with "$genres" in line $retrieve = mysql_query("SELECT * FROM movie WHERE category_id = $genres"); which it may not support string type.
if the table header is not visible the problem is in query, check the value of $genres:
$query = "SELECT * FROM movie WHERE category_id = $genres";
$retrieve = mysql_query($query);
if($retrieve){
....
}else{
print '<pre>'.mysql_error().'</pre>';
print '<pre>'.$query.'</pre>';
}
best regards
|
|