Languages :: PHP :: Fatal error: [] operator not supported for strings |
|||
| By: PHP newbee |
Date: 16/06/2003 00:00:00 |
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 | 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 | 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 | Type : Answer |
|
| And I would suggest to declare $select_all=array(); beforehand :D |
|||
| By: octupul | Date: 16/06/2003 06:55:00 | Type : Comment |
|
| Excellent point VGR it is always better to be safe than sorry :P |
|||
| By: sumotimor | Date: 16/06/2003 07:58:00 | 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 | 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 | 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 | Type : Comment |
|
| octopul : actually you're are wrong... for arrays only. |
|||
| By: VGR | Date: 16/06/2003 15:17:00 | 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 | 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 | 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 | 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 | 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 |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








