visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: PHP :: Fatal error: [] operator not supported for strings


By: PHP newbee U.S.A.  Date: 16/06/2003 00:00:00  English  Points: 50 Status: Answered
Quality : Excellent
I am trying to create an array filled with all the id's returned from a mysql query. To create my array of id's I use the following code:

while ($row = mysql_fetch_array($result)){
$select_all[] = $row[id];
}

I am receiving the following error message:

Fatal error: [] operator not supported for strings

On the line containing $select_all[]=$row[id];

I am having trouble finding specific documentation on how to prevent this error.
By: octupul Date: 16/06/2003 04:09:00 English  Type : Comment
when using mysql fucntions such as teh result function it is ok to use un quoted strings because php will handle them as mixed variables however when accesing information using an associative array quotations must be used

ex:
while ($row = mysql_fetch_array($result)){
$select_all[] = $row["id"];
}
By: octupul Date: 16/06/2003 04:12:00 English  Type : Comment
And after correcting that syntax I would also suggest changing the script in this way

Sorry I reposted I didn't see the other problem till after I hit submit

$count = 0;
while ($row = mysql_fetch_array($result)){
$select_all[$count] = $row["id"];
$count++;
}

This will fill the array select_all with a standard array going from $select_all[0] to $select_all[n] where n is the number of rows returned from your sql query
By: VGR Date: 16/06/2003 06:32:00 English  Type : Answer
And I would suggest to declare $select_all=array(); beforehand :D
By: octupul Date: 16/06/2003 06:55:00 English  Type : Comment
Excellent point VGR it is always better to be safe than sorry :P
By: sumotimor Date: 16/06/2003 07:58:00 English  Type : Comment
The problem is, $select_all is set (to a string) somewhere previously in one of your scripts. As VGR pointed out, declaring $select_all = array(); first should fix it.

You actually can use brackets with a string variable, to get the character at a position:

$name = "Sam";
echo $name[1]; // prints "a"

but that's not what you want in this case.
By: octupul Date: 16/06/2003 08:50:00 English  Type : Comment
sumotimor: actually you are wrong....due to the fact that php is a scripting language you can move beetween diffrent variable types with no problem generally however his problem began with the syntax error found in this line '$select_all[] = $row[id];' because, assigining the falues of an array in this fashion is not allowed by php...you must specify an inner argument $array[integer or a string]....and eventhough it is a very good idea to initialize your variable to make sure you don't receive anomiolas results it is NOT required that you do so
By: sumotimor Date: 16/06/2003 09:25:00 English  Type : Comment
Octopul: This will replicate the fatal error (erator not supported for strings), because the $variable[] syntax is reserved for arrays.

<?php
$string = "hi there";
var_dump($string);
$string[1] = "o"; // sets a character
var_dump($string);
$string[] = "!"; // fatal error!
?>

This will _not_ generate an error, because the $string is now an array:

<?php
$string = "hi there";
var_dump($string);
$string = array("h","i"," ","t","h","e","r","e");
$string[] = "!";
var_dump($string);
?>

So, while using quotes around the array assignment is always a very good idea, that's not the problem in this case. You can't pass a string to something that expects an array. While PHP can convert between numeric & string values automatically, an array is a different animal entirely.
By: VGR Date: 16/06/2003 15:16:00 English  Type : Comment
octopul : actually you're are wrong... for arrays only.
By: VGR Date: 16/06/2003 15:17:00 English  Type : Comment
it's a "special case" where the polymorphic ability of PHP is somewhat limited (or has to be "helped" a bit with declarations, for instance. Follow the online documentation's style and example and eveerything will be fine)

You're right in 99% of the casess... but not for arrays.
By: VGR Date: 16/06/2003 15:18:00 English  Type : Comment
the best proof being that you receive an error message from PHP for an otherwise perfectly valid statement :D :D :D
By: gizmola Date: 17/06/2003 05:40:00 English  Type : Comment
I don't know what the structure of the select_all array is suppossed to be, but as stated less concisely, the [] operator expects to create a new array or add to an existing one. Hence this simple change would fix the parsing problem, and $select_all would be an array of the values of the id column from the result set:

while ($row = mysql_fetch_array($result)){
$select_all[] = array("$row[id]");
}

Meanwhile this would actually create an array of the result set "arrays".

while ($row = mysql_fetch_array($result)){
$select_all[] = array($row);
}

echo "id:". $select_all[0]['id']; // should return the value of id, or any other column for that row, substituting the correct column name for 'id' of course.


By: sumotimor Date: 17/06/2003 06:38:00 English  Type : Comment
not quite gizmola, the problem is the left-hand side:

$select_all[]

because it's previously defined as a string (before the code snippet). This will work:

<?php
$select_all = array();
while ($row = mysql_fetch_array($result)){
$select_all[] = $row[id];
}
?>

but make sure you don't need the value that was in $select_all before!
By: PHP newbee Date: 17/06/2003 07:12:00 English  Type : Comment
Thanks for all the comments everyone. Forcing $select_all to an array has solved the problem. I am still not sure where I assigned a string to $select_all but it could be anywhere (It is a session variable).

Thanks for helping me to understand the problem so I can prevent it from occuring in the future.

Do register to be able to answer

EContact
browser fav
page generated in 378.947020 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page