Page 1 of 1

Getting [] not supported for string but its an array

Posted: Tue Jul 29, 2003 4:14 pm
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;

?>

Posted: Tue Jul 29, 2003 5:02 pm
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.