By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,751 Members | 1,149 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,751 IT Pros & Developers. It's quick & easy.

Warning: Invalid argument supplied for foreach()

P: 3
I got this error TWICE on my script and i don't know why

here is my script:

[PHP]<?php

require_once("db.inc.php");

?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>ForumTesting!</title>

</head>
<body>
<div align="center">
<h2>Current Tests</h2>
<?php

// Get List of Current Tests
$query = mysql_query("SELECT * FROM tests WHERE `finished`=0 AND `active`=1");

if ( !$query )
{
echo "Error, Could not get Tests!";
}
else
{
// Make Loop for Table
echo "<table>";
echo "<tr>";
echo "<th>Name</th>";
echo "<th>System</th>";
echo "<th>Version</th>";
echo "<th>Test Details</th>";
echo "<th>Status</th>";
echo "</tr>";
echo "<tr>";

$current = mysql_fetch_array($query);
foreach( $current as $test )
{
if ( $test['status']=="open" )
{
echo "<td><a href=\"" . $test['link'] . "\" title=\"\" target=\"_blank\">" . $test['name'] . "<img src=\"sys/icons/link_go.png\" alt=\"\" border=\"0\" /></a></td>";
}
elseif ( $test['status']=="closed" )
{
echo "<td>" . $test['name'] . "<img src=\"sys/icons/lock.png\" alt=\"\" border=\"0\" /></td>";
}

echo "<td>" . $test['system'] . "</td>";
echo "<td>" . $test['version'] . "</td>";
echo "<td>" . $test['details'] . "</td>";

if ( $test['status']=="open" )
{
echo "<td><img src=\"sys/icons/lock_open.png\" alt=\"\" /></td>";
}
elseif ( $test['status']=="closed" )
{
echo "<td><img src=\"sys/icons/lock.png\" alt=\"\" /></td>";
}
}

echo "</tr>";
}

?>
<br /><br />
<h2>Old Tests</h2>
<?php

// Get List of Old Tests
$query2 = mysql_query("SELECT * FROM tests WHERE finished = '0' && active = '1'");

if ( !$query2 )
{
echo "Error, Could not get Results";
}
else
{
// Make Loop for Table
echo "<table>";
echo "<tr>";
echo "<th>Name</th>";
echo "<th>System</th>";
echo "<th>Version</th>";
echo "<th>Test Details</th>";
echo "<th>Result</th>";
echo "</tr>";
echo "<tr>";

$current2 = mysql_fetch_array($query2);
foreach( $current2 as $test )
{
if ( $test['status']=="open" )
{
echo "<td>" . $test['name'] . "<img src=\"sys/icons/stop.png\" alt=\"\" border=\"0\" /></td>";
}
elseif ( $test['status']=="closed" )
{
echo "<td>" . $test['name'] . "<img src=\"sys/icons/lock.png\" alt=\"\" border=\"0\" /></td>";
}

echo "<td>" . $test['system'] . "</td>";
echo "<td>" . $test['version'] . "</td>";
echo "<td>" . $test['details'] . "</td>";
echo "<td>" . $test['result'] . "</td>";

}

echo "</tr>";
}

?>
</div>
</body>
</html>[/PHP]

Its on lines 37 and 91

Can anyone solve my issue?
Oct 28 '08 #1
Share this Question
Share on Google+
7 Replies


Markus
Expert 5K+
P: 6,050
Looks okay to me.

Do a print_r() on $current and $current2 before you go into the foreach and show us the results. It sounds like you're not getting any results passed into the array.
Oct 28 '08 #2

P: 3
i did a print_r and it outputs nothing
Oct 28 '08 #3

Markus
Expert 5K+
P: 6,050
i did a print_r and it outputs nothing
Therefore, your mysql_query is returning no results.
Oct 28 '08 #4

Atli
Expert 5K+
P: 5,058
Hi.

I'm guessingthat you do not fully understanding how to use the mysql_fetch_array function.

You are using it as if it should return the entire table. It does not.
It only returns the next row in the result set.
So, your code would only ever print a single database row, but it would add a new HTML table row for each of the columns in the database row.

If you wanted to print the contents of an entire table, you would do:
Expand|Select|Wrap|Line Numbers
  1. // Assuming there is already an open MySQL connection
  2. $result = mysql_query("SELECT * FROM myTable");
  3.  
  4. echo "<table">
  5. while($row = mysql_fetch_array($result)) {
  6.   echo "<tr>";
  7.   foreach($row as $column) {
  8.     echo "<td>$column</td>";
  9.   }
  10.   echo "</tr>";
  11. }
  12. echo "</table>";
  13.  
Calling the mysql_fetch_array function in a loop until there is no more data to be read from the database result.
Oct 28 '08 #5

P: 3
Hi.

I'm guessingthat you do not fully understanding how to use the mysql_fetch_array function.

You are using it as if it should return the entire table. It does not.
It only returns the next row in the result set.
So, your code would only ever print a single database row, but it would add a new HTML table row for each of the columns in the database row.

If you wanted to print the contents of an entire table, you would do:
Expand|Select|Wrap|Line Numbers
  1. // Assuming there is already an open MySQL connection
  2. $result = mysql_query("SELECT * FROM myTable");
  3.  
  4. echo "<table">
  5. while($row = mysql_fetch_array($result)) {
  6.   echo "<tr>";
  7.   foreach($row as $column) {
  8.     echo "<td>$column</td>";
  9.   }
  10.   echo "</tr>";
  11. }
  12. echo "</table>";
  13.  
Calling the mysql_fetch_array function in a loop until there is no more data to be read from the database result.
I have tried your theory and it hasn't worked, it doesn't come up with an error it comes up with some weird random letters ??

Also, I have now put some things in the database as it was empty before
Oct 28 '08 #6

Markus
Expert 5K+
P: 6,050
I have tried your theory and it hasn't worked, it doesn't come up with an error it comes up with some weird random letters ??

Also, I have now put some things in the database as it was empty before
If it was empty, but now has data, try again with your original attempt.
Oct 28 '08 #7

Atli
Expert 5K+
P: 5,058
I have tried your theory and it hasn't worked, it doesn't come up with an error it comes up with some weird random letters ??

Also, I have now put some things in the database as it was empty before
What I said wasn't a theory. All of the mysql_fetch functions return a single row each time they are called. Depending on which one of them you use, they will return an array containing the columns for that row.

The mysql_fetch_assoc returns an associative array, using the name of the column as they element key.
The mysql_fetch_row function returns a zero-indexed array.
The mysql_fetch_array is a combination of both, basically merging the results of the other two and returning that.


Show us the results you are getting and the code that you are using and we can try to help find out what you did wrong.

Note that the code I posted earlier has a syntax error. If your code was based on it you may want to check it out. (The error is in the line that prints the table tag).
Oct 29 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.