Bluehost.com Web Hosting $6.95

The return of mysql_fetch_assoc()

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"]; ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-30-2003
Ney andré de mello zunino
 
Posts: n/a
Default The return of mysql_fetch_assoc()

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

Reply With Quote
  #2 (permalink)  
Old 07-30-2003
Ray Hunter
 
Posts: n/a
Default Re: [PHP] The return of mysql_fetch_assoc()


> $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


Reply With Quote
  #3 (permalink)  
Old 07-30-2003
Curt Zirzow
 
Posts: n/a
Default Re: [PHP] The return of mysql_fetch_assoc()

* 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."
Reply With Quote
  #4 (permalink)  
Old 07-30-2003
John W. Holmes
 
Posts: n/a
Default Re: [PHP] The return of mysql_fetch_assoc()

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




Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 12:05 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0