Unix Technical Forum

Mysql result to array

This is a discussion on Mysql result to array within the MySQL forums, part of the Database Server Software category; --> I have this query, it gives the suspected information in myphpadmin: select listid, count(*) from table1 group by listid ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 07:15 AM
Jaak
 
Posts: n/a
Default Mysql result to array

I have this query, it gives the suspected information in myphpadmin:

select listid, count(*) from table1 group by listid

How do I put this the result into an array in php? I have problems with the
count(*) field


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 07:15 AM
Justin Koivisto
 
Posts: n/a
Default Re: Mysql result to array

Jaak wrote:
> I have this query, it gives the suspected information in myphpadmin:
>
> select listid, count(*) from table1 group by listid
>
> How do I put this the result into an array in php? I have problems with the
> count(*) field
>
>


$q='select listid, count(*) from table1 group by listid';
$res=mysql_query($q);
if($res){
$data=array();
while($row=mysql_fetch_array($res,MYSQL_NUM)){
$data[]=array(
'listid' => $row[0],
'count' => $row[1]
);
}
}

Untested, but it should give you the idea... Another option is to change
the query to something similar to:

select listid, count(*) as num_found from table1 group by listid

Then if you are using mysql_fetch_assoc, then the key name will be
'num_found'

HTH

--
Justin Koivisto, ZCE - justin@koivi.com
http://koivi.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 07:16 AM
Jaak
 
Posts: n/a
Default Re: Mysql result to array


"Justin Koivisto" <justin@koivi.com> schreef in bericht
news:dLGdnV0pDpb1jFjeRVn-rg@onvoy.com...
> Jaak wrote:
>> I have this query, it gives the suspected information in myphpadmin:
>>
>> select listid, count(*) from table1 group by listid
>>
>> How do I put this the result into an array in php? I have problems with
>> the
>> count(*) field
>>
>>

>
> $q='select listid, count(*) from table1 group by listid';
> $res=mysql_query($q);
> if($res){
> $data=array();
> while($row=mysql_fetch_array($res,MYSQL_NUM)){
> $data[]=array(
> 'listid' => $row[0],
> 'count' => $row[1]
> );
> }
> }
>
> Untested, but it should give you the idea... Another option is to change
> the query to something similar to:
>
> select listid, count(*) as num_found from table1 group by listid
>
> Then if you are using mysql_fetch_assoc, then the key name will be
> 'num_found'

Thanks Justin, it really helped me! I was confused with the 'count' thing,
because this was not really a field in the table.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:01 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com