www.webdeveloper.com
Results 1 to 5 of 5

Thread: Warning: Invalid argument supplied for foreach()

  1. #1
    Join Date
    Mar 2012
    Posts
    14

    Warning: Invalid argument supplied for foreach()

    I'm very new to PHP. I'm getting this message on this page and just don't understand why?

    Thanks for your help....
    --------
    <?php
    //Sample Database Connection Syntax for PHP and MySQL.

    //Connect To Database

    $hostname="";
    $username="";
    $password="";
    $dbname="";

    mysql_connect($hostname,$username, $password) or die ("<html><script language='JavaScript'>alert('Unable to connect to database! Please try again later.'),history.go(-1)</script></html>");
    mysql_select_db($dbname);

    # Check If Record Exists

    $query = "SELECT * FROM players
    WHERE playeryear = 2011
    ORDER BY number";

    $result = mysql_query($query);
    ?>

    <?php foreach ($result as $playerslist) { ?>
    <tr>
    <td><?php echo $playerslist['playersname']; ?></td>
    <td><?php echo $playerslist['number']; ?></td>
    <td><?php echo $playerslist['playerpos']; ?></td>
    </tr>
    <?php } ?>
    -----------

    Any help would be greatly appreciated...

  2. #2
    Join Date
    Jul 2007
    Location
    Wisconsin
    Posts
    468
    Chek your SQL query. It's likely that it's not retuning what your expecting, thus $result is not an array.

  3. #3
    Join Date
    Feb 2012
    Posts
    218
    You are using it wrong.
    Code:
    $query = "SELECT * FROM players
     WHERE playeryear = 2011
     ORDER BY number";
    
     $result = mysql_query($query);
    
     while ($playerslist = mysql_fetch_assoc($result)) { ?>
     <tr>
     <td><?php echo $playerslist['playersname']; ?></td>
     <td><?php echo $playerslist['number']; ?></td>
     <td><?php echo $playerslist['playerpos']; ?></td>
     </tr>
     <?php } ?>
    You forgot to call mysql_fetch_assoc

  4. #4
    Join Date
    Mar 2012
    Posts
    14
    That doesn't surprise me...

    Thanks for your help I'll try it tonight.

  5. #5
    Join Date
    Mar 2012
    Posts
    14
    Thanks it did work!!!
    Here is what the text book originally told me to write it....
    -----
    <?php
    //Sample Database Connection Syntax for PHP and MySQL.

    //Connect To Database

    $hostname="";
    $username="";
    $password="";
    $dbname="";

    mysql_connect($hostname,$username, $password) or die ("<html><script language='JavaScript'>alert('Unable to connect to database! Please try again later.'),history.go(-1)</script></html>");
    mysql_select_db($dbname);

    # Check If Record Exists

    $query = "SELECT * FROM players
    WHERE playeryear = 2011
    ORDER BY number";
    $players = $db->query($query);

    ?>

    <?php foreach ($players as $playerlist) : ?>
    <tr>
    <td><?php echo $playerlist['playersname']; ?></td>
    <td><?php echo $playerlist['number']; ?></td>
    <td><?php echo $playerlist['playerpos']; ?></td>
    <td><?php echo $playerlist['height']; ?></td>
    <td><?php echo $playerlist['weight']; ?></td>
    <td><?php echo $playerlist['grade']; ?><br></td>
    </tr>
    <?php endforeach; ?>
    ----

    I get this error..
    Fatal error: Call to a member function query()
    Line 19 which is ...

    $players = $db->query($query);

    How should it really be written using the example I was testing...
    Thanks for all your help....

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles