Getting [] not supported for string but its an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
like_duh44
Forum Commoner
Posts: 63
Joined: Sat Jul 26, 2003 6:57 pm

Getting [] not supported for string but its an array

Post by like_duh44 »

I have code that gets users from a mysql database table 'chat_users' and puts them into an array. I then have it display the users in a foreach() command. It gives me an error saying the [] operator not supported for strings, but the query gives an array. Whats wrong? Heres the code:

Code: Select all

<?

$name = $_GET&#1111;username];


    $server = "not shown for security reasons";
    $database = "not shown for security reasons";
    $user = "not shown for security reasons";
    $password = "not shown for security reasons";

    $link = mysql_connect($server, $user, $password)
        or die("Could not connect");
    mysql_select_db($database, $link) or die("Could not select database");

    $userquery = "SELECT * FROM chat_users"
    $userresult = mysql_query($userquery, $link) or die("Query failed Number 1");

    while ($row2 = mysql_fetch_row($userresult))
    &#123;
        $user&#1111;]=$row2;
    &#125;

    mysql_free_result($userresult);

echo "<b/>Users Online:</b><br/>";

foreach ($user as $poster)
&#123;
$username = $poster&#1111;"user"];
echo "- $username<br/>";
&#125;

?>
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post by pootergeist »

in your foreach the $poster becomes the value of each $user - so you'd need to either run a sub-foreach or assign the $index and array that

foreach($user AS $index=>$values)
{
echo $index['user']. '<br />';
}

should work.
Post Reply