tags:

views:

69

answers:

7

Here is the code:

<?php

//Starting session

session_start();

//Includes mass includes containing all the files needed to execute the full script
//Also shows homepage elements without customs

require_once ('includes/mass.php');

$username = $_SESSION['username'];

if (isset($username))

{   

//Query database for the users networths

$sq_l = "SELECT * FROM user ORDER BY worth";

$sql_query_worth = mysql_query($sq_l);

while ($row = mysql_fetch_assoc($sql_query_worth))

      {

         $dbusername = $row['username'];

         $dbworth    = $row['worth'];

            foreach ($dbusername as $dbuser)

                {
                    echo ". USER: ".$dbuser." Has a networth of:  ".$dbworth;

                }
      }               
}

?>

There are three results. And here are the errors.

Here is the error msg:

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\Mogul\richlist.php on line 32

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\Mogul\richlist.php on line 32

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\Mogul\richlist.php on line 32

+5  A: 

Just change:

        foreach ($dbusername as $dbuser)

            {
                echo ". USER: ".$dbuser." Has a networth of:  ".$dbworth;

            }

to:

echo ". USER: ".$dbusername." Has a networth of:  ".$dbworth;
klausbyskov
A: 

You are trying to iterate through a string like an array - foreach requires array() input

Andy
+3  A: 

That's because you aren't passing an array to the foreach loop. Try this:

while ($row = mysql_fetch_assoc($sql_query_worth))
{
    $dbusername = $row['username'];
    $dbworth    = $row['worth'];
    echo "USER: ".$dbusername ." Has a networth of:  ".$dbworth;              
}
John Conde
+1  A: 

foreach() iterates over an array. Most likely $dbusername is a string instead, which means that you should get rid of the foreach() and just echo $dbusername directly instead.

Ignacio Vazquez-Abrams
A: 

No need for your foreach loop ; this should work better :

while ($row = mysql_fetch_assoc($sql_query_worth))
{
  $dbusername = $row['username'];
  $dbworth    = $row['worth'];
  // The username is in $dbusername
  // And the worth is in $dbworth
  // So, just use those :
  echo ". USER: ".$dbusername." Has a networth of:  ".$dbworth;     
}


Bascially :

  • mysql_fetch_assoc fetches the data of the current row into $row
  • which means $row is an array, that contains the username and worth keys
  • you are assigning the values of those two array entries to $dbusername and $dbworth
    • which now contains strings
    • and can be used directly : they don't contain any sub-array.


If you want to get a better look to what your variables contain, you could use print_r, or var_dump ; for instance, like this :

while ($row = mysql_fetch_assoc($sql_query_worth))
{
  var_dump($row;)
  $dbusername = $row['username'];
  $dbworth    = $row['worth'];
  var_dump($dbusername, $dbworth);
  echo ". USER: ".$dbusername." Has a networth of:  ".$dbworth;     
}
Pascal MARTIN
A: 

Why do you need the foreach there? Thats not an array. You should have this outside the foreach.

echo ". USER: ".$dbuser." Has a networth of: ".$dbworth;

aip.cd.aish
A: 

You are calling foreach incorrectly. It expects an array as the first argument. In this case just remove the foreach as you already have a while loop iterating through your database query results.

ar