Displaying information stored in MySQL with PHP

I am trying to set up this page to display the information stored in my MySQL database using PHP. I keep getting an “invalid argument for foreach()” which I know means that the array I am trying to use does not exist. I can’t figure out why though because I have the array setup in my controller. Please advise.

PHP Controller:

<?php
include 'magicquotes.inc.php';

//Display players list
include 'db.inc.php';
$result = mysqli_query($link, 'SELECT id, number, name, team, height, weight FROM roster');
if(!result)
{
	$error = 'Error fetching players from database.';
	include 'error.html.php';
	exit();
}

while ($row = mysqli_fetch_array($result))
{
	$rosters[] = array('id' => $row['id'], 'number' => $row['number'], 'name' => $row['name'], 'team' => $row['team'], 'height' => $row['height'], 'weight' => $row['weight']);
}

include '../roster.php';
																 
?>

HTML/PHP Code:

<?php include_once('includes/helpers.inc.php'); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Har-Ber High School Wildcat Baseball Roster</title>
<meta name="keywords" content="Har-Ber high school Wildcat Wildcats baseball schedule">
<meta name="description" content="Har-Ber High School Wildcat Baseball Official Website.">
<meta http-equiv="Content-Language" content="en-us">
<link rel="stylesheet" type="text/css" href="css/style.css" media="all">
<link rel="stylesheet" type="text/css" href="css/interior.css" media="all">
</head>

<body id="body">

	<table id="pageWrapper">

        <tr>

        <td id="navigation" colspan="2">

            <div>

            <?php include('include/navigation.php'); ?>

			</div>

        </td>

        </tr>

        <tr>

        <td id="header" colspan="2">

			<div>

			<?php include('include/header.php'); ?>

			</div>

        </td>

        </tr>

		<tr>

			<td>

	      	<table id="contentWrapper">

	      	<tr>

	      	<td class="leftContainer" valign="top">

	      	<div class="interiorContent">

            <h1>2011 Har-Ber High School Wildcats Roster</h1>

            <ul class="rosterSelect">

            	<li><a class="current" href="roster.php">All</a></li>

                <li><a href="varsity_roster.php">Varsity</a></li>

                <li><a href="jv_roster.php">JV</a></li>

                <li class="last"><a href="bw_roster.php">B/W</a></li>

            </ul>
			<?php foreach ($rosters as $roster): ?>
            <form action="" method="post">
			<table class="infoTable" cellpadding="0" cellspacing="0">

            <tr class="tableHeader">

            	<td colspan="6">2011 Har-Ber Wildcat Roster</td>

            </tr>

            <tr class="tableWhite">

            	<td width="50px">Number</td>
                <td width="200px">Name</td>
                <td width="150px">Team</td>
                <td width="50px">Height</td>
                <td width="50px">Weight</td>
                <td width="100px">Action</td>

            </tr>

            <tr class="tableGrey">

            	<td><?php htmlout($roster['number']); ?></td>
                <td><?php htmlout($roster['name']); ?></td>
                <td><?php htmlout($roster['team']); ?></td>
                <td><?php htmlout($roster['height']); ?></td>
                <td><?php htmlout($roster['weight']); ?></td>
                <td><input type="hidden" name="id" value="<?php echo $roster['id'];?>" >
                <input type="submit" name="action" value="Edit">
        		<input type="submit" name="action" value="Delete"></td>

            </tr>
            </table>
            </form>
      		<?php endforeach; ?>

	        </div>

	        </div>

	        </td>

	        <td class="rightContainer" valign="top">

	        <?php include('include/upcomingSchedule.php'); ?>

	       </td>

	    </tr>

	  </table>
      
	</td>

</tr>

</table>

</body>



</html>

You can view the results (and the error I am receiving) here:

http://harberwildcatbaseball.com/roster.php

Add a var dump to see what $rosters really looks like.


while ($row = mysqli_fetch_array($result))
{
	$rosters[] = array('id' => $row['id'], 'number' => $row['number'], 'name' => $row['name'], 'team' => $row['team'], 'height' => $row['height'], 'weight' => $row['weight']);
}
var_dump($rosters);

include '../roster.php';
																 
?>

Do you get a result set if you execute the query in PhpMyAdmin?

Very strange but when I added the var_dump, it did not display once I reloaded the page…not really sure why…

I am not very familiar with PHPMyAdmin yet so what exactly do I need to input in to the query?

Just paste the query (SELECT id, number, name, team, height, weight FROM roster) into another MySQL tool (doesn’t have to be PhpMyAdmin) to see that the query is working.

Are there rows in the table?

Thanks for the help cranial-bore…I found the problem. I had some paths linked to the wrong directories (that is what I get for working so many hours straight). Thanks for the help again.