Reputation: 1511
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
Upvotes: 0
Views: 184
Reputation: 36999
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.
Upvotes: 0
Reputation: 9714
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;
Upvotes: 0
Reputation: 400972
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
$row
is an array, that contains the username
and worth
keys$dbusername
and $dbworth
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;
}
Upvotes: 0
Reputation: 798606
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.
Upvotes: 1
Reputation: 219804
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;
}
Upvotes: 3
Reputation: 17771
You are trying to iterate through a string like an array - foreach requires array() input
Upvotes: 0
Reputation: 120937
Just change:
foreach ($dbusername as $dbuser)
{
echo ". USER: ".$dbuser." Has a networth of: ".$dbworth;
}
to:
echo ". USER: ".$dbusername." Has a networth of: ".$dbworth;
Upvotes: 5