473,890 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Warning: Invalid argument supplied for foreach()

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

here is my script:

[PHP]<?php

require_once("d b.inc.php");

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

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

// Get List of Current Tests
$query = mysql_query("SE LECT * 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>Versio n</th>";
echo "<th>Test Details</th>";
echo "<th>Status </th>";
echo "</tr>";
echo "<tr>";

$current = mysql_fetch_arr ay($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("SE LECT * 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>Versio n</th>";
echo "<th>Test Details</th>";
echo "<th>Result </th>";
echo "</tr>";
echo "<tr>";

$current2 = mysql_fetch_arr ay($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
7 1720
Markus
6,050 Recognized Expert Expert
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
Jamesking56
3 New Member
i did a print_r and it outputs nothing
Oct 28 '08 #3
Markus
6,050 Recognized Expert Expert
i did a print_r and it outputs nothing
Therefore, your mysql_query is returning no results.
Oct 28 '08 #4
Atli
5,058 Recognized Expert Expert
Hi.

I'm guessingthat you do not fully understanding how to use the mysql_fetch_arr ay 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_arr ay function in a loop until there is no more data to be read from the database result.
Oct 28 '08 #5
Jamesking56
3 New Member
Hi.

I'm guessingthat you do not fully understanding how to use the mysql_fetch_arr ay 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_arr ay 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
6,050 Recognized Expert Expert
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
5,058 Recognized Expert Expert
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_ass oc 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_arr ay 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

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

Similar topics

35
3245
by: jerrygarciuh | last post by:
Hi all, I was just wondering what popular opinion is on PHP giving this warning: Warning: Invalid argument supplied for foreach() in /home/boogerpic/public_html/my.php on line 6 when presented with an indefined variable. It makes sense to me to warn if an unacceptably defined var is passed but it
4
7844
by: Ryanlawrence1 | last post by:
Heya, I get these 2 errors: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/themepar/public_html/changepass.php on line 20 You have not entered all the fields Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/themepar/public_html/changepass.php on line 34 Sorry You failed to enter the correct old password I was wondering if anyone could help me, I have...
2
16951
by: techjohnny | last post by:
Error: Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/jplane/certcent/phpweb/quiz/index.php on line 20 Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/jplane/certcent/phpweb/quiz/index.php on line 21 PHP CODE:
1
2719
by: lsmamadele | last post by:
I am getting the following error messages in my search: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/mamadele/public_html/BESTPLAYS/search.php on line 113 Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/mamadele/public_html/BESTPLAYS/search.php on line 127 My code is below. Any help would be much appreciated. ...
11
3621
by: Breana | last post by:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/breana/public_html/category.php on line 88 ------------------------------------------- It does this when there is no result "empty table" how can i do a quick fix to say No Results... row 88: if ($myrow = mysql_fetch_array($result)) { do { if ($rowcolor == 1) {
2
10300
by: yogitha | last post by:
For the below code i am getting an error like : --------------------------------------------------------------- Warning: fread(): supplied argument is not a valid stream resource in D:\Public_Site\multifileupload\welcome_addfiles.php on line 28 ------------------------------------------------------------ i am trying many ways by giving diff arguments in fread() --------------------------------------------------- suggest me , wat error...
1
1542
by: custom3 | last post by:
Hello, I keep getting the following warning: "PHP Warning: Invalid argument supplied for foreach() in /home/csiexecu/public_html/modules/mod_mainmenu.php on line 339" I am a novice Joomla user that has just upgraded to 1.0.13. Can anyone give me direction on how or to whom I should go to fix this problem? The home page intermittently loads and times out. Thanks!
1
1360
by: guoxin | last post by:
Hi All, May i know how to rectify the following php error? Thanks folks Warning: Invalid argument supplied for foreach() in /home/alan/do_add-user.php on line 36 do_add-user.php.php on line 304: $project_array = $_POST;
1
1644
by: BryanA | last post by:
Something seems to be wrong with this function and I can't figure out why it is failing. The error that it gives is; Invalid argument supplied for foreach() . function transposition($words){ foreach($words as $word){ for($x=0;$x<strlen($word)-1;$x++){ $results = substr($word, 0, $x) . $word . $word . substr($word, $x+2, strlen($word));
14
3362
by: rashgang | last post by:
i have dynamically created checkbox which will like yahoo mail if check box selected it will highlight a row but deleteall function is not working plz chek it function doDeleteAll($objArray){ foreach($objArray as $strkey=>$strValue) { $strDeleteId = $strValue; doDelete($strDeleteId);
0
10799
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10446
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8004
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7154
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5832
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6032
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4655
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3263
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.