For Programmers: Free Programming Magazines  


Home > Archive > PHP SQL > August 2007 > mysql_fetch_array yielding different results to MySQL's output









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author mysql_fetch_array yielding different results to MySQL's output
J_K9

2007-07-17, 8:01 am

Hi,

I'm having a problem with a web app I am developing. The code in
question is:

$sql = "SELECT groupID FROM memberships WHERE userID = ".$uid;
$result = mysql_query($sql);
$result = mysql_fetch_array($result);
print_r($result);

Now, print_r($result) displays the following (when $uid = 1):

Array ( [0] => 1 [groupID] => 1 )

HOWEVER, that's not what it should be. If I run the same command at
the MySQL CLI I get the following:

+---------+
| groupID |
+---------+
| 1 |
| 2 |
+---------+

So, if I'm not mistaken, the $result array should contain ([0] => 1,
[1] => 2), NOT what PHP says it does. Can someone please tell me why
this is happening and how to correct it? I need to use the data
outputted by MySQL in my PHP script but if PHP is not receiving the
same data then I have no way of using it.

Thanks,

-Max

Harrie Verveer

2007-07-17, 8:01 am

Hi Max,

mysql_fetch_array($result) returns only 1 record from your result. Try:

while($row = mysql_fetch_array($result))
{
print_r($row); // row is 1 record from the memberships table
}

Kind regards,

Harrie Verveer
--
http://www.ibuildings.nl/blog/authors/Harrie-Verveer

J_K9 wrote:
> Hi,
>
> I'm having a problem with a web app I am developing. The code in
> question is:
>
> $sql = "SELECT groupID FROM memberships WHERE userID = ".$uid;
> $result = mysql_query($sql);
> $result = mysql_fetch_array($result);
> print_r($result);
>
> Now, print_r($result) displays the following (when $uid = 1):
>
> Array ( [0] => 1 [groupID] => 1 )
>
> HOWEVER, that's not what it should be. If I run the same command at
> the MySQL CLI I get the following:
>
> +---------+
> | groupID |
> +---------+
> | 1 |
> | 2 |
> +---------+
>
> So, if I'm not mistaken, the $result array should contain ([0] => 1,
> [1] => 2), NOT what PHP says it does. Can someone please tell me why
> this is happening and how to correct it? I need to use the data
> outputted by MySQL in my PHP script but if PHP is not receiving the
> same data then I have no way of using it.
>
> Thanks,
>
> -Max
>

Rik

2007-07-17, 7:01 pm

On Tue, 17 Jul 2007 13:14:04 +0200, J_K9 <aib.wolphin@gmail.com> wrote:

> Hi,
>
> I'm having a problem with a web app I am developing. The code in
> question is:
>
> $sql =3D "SELECT groupID FROM memberships WHERE userID =3D ".$uid;
> $result =3D mysql_query($sql);
> $result =3D mysql_fetch_array($result);
> print_r($result);
>
> Now, print_r($result) displays the following (when $uid =3D 1):
>
> Array ( [0] =3D> 1 [groupID] =3D> 1 )
>
> HOWEVER, that's not what it should be. If I run the same command at
> the MySQL CLI I get the following:
>
> +---------+
> | groupID |
> +---------+
> | 1 |
> | 2 |
> +---------+
>
> So, if I'm not mistaken, the $result array should contain ([0] =3D> 1,=


> [1] =3D> 2), NOT what PHP says it does. Can someone please tell me why=


> this is happening and how to correct it? I need to use the data
> outputted by MySQL in my PHP script but if PHP is not receiving the
> same data then I have no way of using it.


RTFM:
mysql_fetch_array():
1. Returns a single row from the result, use a while loop to get all =

result.
2. Returns a both numerically and associative array by default. Either u=
se =

the constants MYSQL_NUM or MYSQL_ASSOC, or the function mysql_fetch_row(=
) =

or mysql_fetch_assoc() respectively if this isn't desired behaviour.

$result =3D mysql_query($sql);
$set =3D array();
while($row =3D mysql_fetch_assoc()) $set[] =3D $row;
print_r($set);

-- =

Rik Wasmus
D14

2007-08-12, 8:42 am

Jessica Alba kneels for doggy XXXX.!
http://www.videomoviesonline.com/player.php?vid=726648

Carmen Electra Banged & Cum Swallowing!
http://www.videomoviesonline.com/Watch?vid=726648

Halle Berry In Thong Shows Butt!
http://www.videomoviesonline.com/Wi...r.php?id=726648

Hilary Swank Dildos Her Pussy!
http://www.videomoviesonline.com/Player.wmv?vid=726648

Halle Berry , Ultimate surrender!
http://www.videomoviesonline.com/Me...er?movie=726648
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com