tags:

views:

67

answers:

8

I think this is just something I haven't come across before!

$r = $database->currentChallengers($f, $g);
        while($row=mysql_fetch_assoc($r))
        {
            $u[]=$row['username'];
            $i[]=$row['userid'];
            $s[]=$row['streak'];
        }

        for ($i = 0; $i < count($u); $i++)
        {
            foreach ($u as $user)
            {
                echo "Challenger: $u[$i]";
            }
        }

That PHP is for the following database query:

function currentChallengers($f, $g)
{
    $q = "SELECT k.userid, k.streak, u.username
          FROM ".TBL_KOTH." k 
          INNER JOIN ".TBL_USERS." u
          ON k.userid = u.id
          WHERE k.format = '$f' && k.game = '$g' && k.king = '0' && k.done = '0'
          ORDER BY k.userid";
    return mysql_query($q, $this->connection);
}

I am getting the error

Fatal error: [] operator not supported for strings

On the line

$u[]=$row['username'];

Could anyone tell me why this is happening? Also, should the code I have written list each username from the query? Thanks

+2  A: 

PHP seems to be assuming your uninitialized variables are strings. See Pekka's Answer. Try initializing/declaring those variables before you start pushing values onto them.

$u = array();
$i = array();
$s = array();
Mike B
Nope, I just tested. If `$u` were uninitialized, PHP would treat it as an array and throw a notice but not a fatal error. This means that `$u` is used elsewhere in the code, and shouldn't be reset without checking carefully first. Or the OP should use meaningful variable names in the first place to avoid such mix-ups.
Pekka
@Pekka interesting, I stand corrected. I'll upvote your answer, hopefully Luke will change the accepted answer.
Mike B
A: 

Try this, instead of arrays :

            $u=$row['username'];
            $i=$row['userid'];
            $s=$row['streak'];
c0mrade
A: 

Are you sure that $u is not already defined? Initialize it with $u = array(); before the first "while"...

Palantir
A: 

The problem seems to come to the fact that $u (and possibly $s and $i) seems to be a string.

Try adding:

$u = array();
$s = array();
$i = array();

before:

$r = $database->currentChallengers($f, $g);
Weboide
+2  A: 

Use meaningfull variable names. You have used $u to store a string somewhere above this code. Probably you stored a username in it.

So I recommend using variables $username and $users.

And set $users = array(); before a loop, as proposed by Mike/

Naktibalda
+1 for good general coding advice
Niels Bom
+1 if you want to obfuscate your code, there are automatic tools for this.
greg0ire
+1  A: 

What are you trying to do with $u[]=$row['username'];? $u is currently a string and you can't write to a specific index of a string (you haven't specified any index either).

If you're trying to store rows in to arrays, declare them as arrays and use an iterating variable to populate it.

$u = array();
$i = array();
$s = array();
$k = 0;
while($row=mysql_fetch_assoc($r))
{
  $u[$k] = $row['username'];
  $i[$k] = $row['userid'];
  $s[$k] = $row['streak'];
  $k = $k + 1;
}
Amarghosh
A: 

You should add $u = array() before accessing it. You probably use some other $u as a string in another part of your script and PHP still believe it's the same one.

kriss
+3  A: 

The other answers already point out what you need to do: Use an array. But before that, check whether $u etc. are being used somewhere else in the code already. You could get in trouble because they may already be in use elsewhere - as strings.

And, you should really use some more verbose variable names to avoid building a maintenance nightmare. Why not use for example

        $username[] = $row['username'];
        $userid[]   = $row['userid'];
        $streak[]   = $row['streak'];
    }
Pekka