This is a discussion on The return of mysql_fetch_assoc() within the PHP General forums, part of the PHP Programming Forums category; Hello. I wonder why the following code is not accepted by the PHP interpreter $fooVar = mysql_fetch_assoc($result)["barColumn"]; ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hello.
I wonder why the following code is not accepted by the PHP interpreter $fooVar = mysql_fetch_assoc($result)["barColumn"]; I am forced to create an intermediary variable for the array returned from mysql_fetch_assoc(), like this: $record = mysql_fetch_assoc($result); $fooVar = $record["barColumn"]; Why is that? It doesn't seem logical. Could it be a bug? Thank you, -- Ney André de Mello Zunino |
|
|||
|
> $fooVar = mysql_fetch_assoc($result)["barColumn"]; No bug here...mysql_fetch_assoc($result) is a function that takes a result set as an argument and returns only one row as an associated array...that is why you need to do > $record = mysql_fetch_assoc($result); > $fooVar = $record["barColumn"]; That is logic when it comes from the database...database results are returned as rows with various column(s). This allows you to go through each row and pull out all the columns for that row. HTH, BigDog |
|
|||
|
* Thus wrote Ney André de Mello Zunino (zunino@unu.edu):
> Hello. > > I wonder why the following code is not accepted by the PHP interpreter > > $fooVar = mysql_fetch_assoc($result)["barColumn"]; Because that is not an acceptable way to assign a variable. > > I am forced to create an intermediary variable for the array returned > from mysql_fetch_assoc(), like this: > > $record = mysql_fetch_assoc($result); > $fooVar = $record["barColumn"]; > > Why is that? It doesn't seem logical. Could it be a bug? Because that is how php works. If you are having that much trouble being able to deal with its rules of coding syntax then I would suggest to go back to your other language and discontinue gripping about it. By comparing php to another language and observing that php is not capable of what the other one can do, does not make it a bug. If you so desire to have this capability in php, you should submit a feature request to the php-development team. And probably explain your reasoning more thoroughly than just 'It is logical to do so.' You might get a positive result back. Curt -- "I used to think I was indecisive, but now I'm not so sure." |
|
|||
|
Ney André de Mello Zunino wrote:
> Ray Hunter wrote: > >>> $fooVar = mysql_fetch_assoc($result)["barColumn"]; >> >> >> No bug here...mysql_fetch_assoc($result) is a function that takes a >> result set as an argument and returns only one row as an associated >> array...that is why you need to do > > > Thanks for the quick response, but I am not sure I understand your > argument. Yes, I am aware that the function returns an array > corresponding to a single row/record; what I don't understand is why I > am not able to apply a key directly to that "temporary" return value. In > other words, it is necessary to "copy" the resulting row into a new > variable so that it is then possible to use the column name (key) to > retrieve the desired value. Use mysql_result() if you want to fetch a single column directly. -- ---John Holmes... Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/ PHP|Architect: A magazine for PHP Professionals – www.phparch.com |