Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Invalid argument supplied for foreach() 1

Status
Not open for further replies.

Qmoto

MIS
Oct 21, 2004
74
US
Could someone please tell me why I am getting an error with this code at line 40... This has been kicking by beginner butt all over town and I'm gettin sick of it!

Line 40 is the one containing:
Code:
foreach ($data as $dat => $d) {
  echo "<td>$d</td>";
  }

Here's the actual code...
Code:
<?php
$host = 'dbServerName';
$user = 'myUser';
$pass = 'myPass';
$db = 'myDatabase';

$c = mssql_connect($host, $user, $pass) or die ('Could not connect to '.$host.' using '.$user.' credentials.');
mssql_select_db($db);

$query = "
SELECT * FROM myDatabase
";

$result = mssql_query($query);
$field_num = mssql_num_fields($result);
$row_num = mssql_num_rows($result);
$result_array = mssql_fetch_assoc($result);

echo "Using Database: $db<br>";
echo "Query: <em>$query</em><br>";
echo "Query Result: $result<br>Field Count: $field_num<br>Row Count: $row_num";
echo "<hr>\r\n\r\n";

$result_keys = array_keys($result_array);
$result_vals = array_values($result_array);

echo "<table border='1'><br>\r\n";
// creates table header row using column names
echo "<tr>\r\n";
foreach ($result_array as $key => $val) {
  echo "<th>$key</th>\r\n";
}
echo "</tr>\r\n\r\n";
// end of th based row

//begin table cells holding row values
for ($row = 0; $row < $row_num; $row++){
  $data = mssql_fetch_row($result);
  echo "<tr>\r\n";
   foreach ($data as $dat => $d) {
   echo "<td>$d</td>";
   }
  echo "\r\n</tr>\r\n";
}
echo "\r\n\r\n</table>\r\n";
?>

Thanks for the help everyone, I really appreciate it!
--Isn't it nice of me to assume you will help ;0)
 
heres a happy ending for you

Code:
//begin table cells holding row values
	while ($myrow = mssql_fetch_array($result)){

  		echo "<tr>\r\n";
		for ($i = 0; $i < ($field_num); $i++) {

			echo "<td> $myrow[$i] </td>\n";

		}

		echo "</tr>\n";

	}
echo "\r\n\r\n</table>\r\n";
?

thhink it should work, be easier to understand and less code.

also you set but don't seem to use :
$result_keys = array_keys($result_array);
$result_vals = array_values($result_array);


______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Thank you very much Karver, I appreciate the quick response and the working code!

This is fact does work, and work nicely. However, could you explain to me why I was getting the error? I'm trying to understand what I was doing wrong...

Is there a significant difference between the following?
Code:
($field_num)
$field_num

And why doesn't this work?
Code:
count($field_num)

I realize that this is simply counting an already numerical value, but why would it cause the return table to be empty except for the key column?


Ohh, and the $result_keys/$result_vals were left over from a previous attempt to get at the data.

Thank again for the help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top