473,327 Members | 2,069 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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
Jan 11 '06 #1
4 7675

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


you could create an alias,
COUNT(*) AS 'number_of_countings'

and later call it as $query['number_of_countings']

Jan 11 '06 #2
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 - ju****@koivi.com
http://koivi.com
Jan 11 '06 #3
frizzle wrote:
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

you could create an alias,
COUNT(*) AS 'number_of_countings'

and later call it as $query['number_of_countings']


heh - I missed this thread in here, but picked up on it in a.c.d.mysql...

--
Justin Koivisto, ZCE - ju****@koivi.com
http://koivi.com
Jan 11 '06 #4

"Justin Koivisto" <ju****@koivi.com> schreef in bericht
news:dL********************@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.
Jan 13 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: W i l l | last post by:
I have Apache 2 parsing and serving PHP files just fine on my winXP box, however, when I try to add some mysql database functionality I get a warning that looks like this: Fatal error: Call to...
2
by: Nik Coughin | last post by:
Today is the first time I've used MySQL with PHP. What is the best way to turn a column into an array? This is how I am doing it at the moment, surely this is sub-optimal: $query = 'SELECT...
0
by: Phil Powell | last post by:
The table already has a fulltext index and from there I can use the MySQL fulltext search query to get results as well as the relevancy score. The problem I have is that MySQL has a default...
9
by: Börni | last post by:
Hi, I have an sql query like this: SELECT column FROM table WHERE column1="3" AND column2="1" This query works perfectly if i run it in the command line, to be exactly it return two results. But...
3
by: auron | last post by:
Hi there, I have a really stupid and banal problem with showing the results of a MySQL query in PHP, preciselly with MySQL count() function that gives to a variable in PHP the result. NOTE:...
6
by: Duderino82 | last post by:
I was wondering if there is a way to collect the names of the fields from a specific table. I think the soluction is to be researched in the sql code but maybe someone knows of a way to o so...
16
by: frizzle | last post by:
Hi there! I'd like to create a function which input is the result of a mySQL query. The output should be exactly the same, only not a mySQL result array, but a 'real' array. So it should also...
7
by: gcox | last post by:
Hello, I am using php and MySQL and have following as part of the code while ($row = mysql_fetch_array($result)) { printf("<td valign='top'><strong>Venue</strong></td><td _ valign='top'>%s...
21
by: bruno_guedesav | last post by:
I've made a function to fetch all results as an array of result- arrays. Getting the result arrays is easy, via mysql_fetch_array, and function itself is quite simple, as follows: function...
3
by: ist | last post by:
Hi, I am trying to get (and transfer over ASP.NET) some encrypted data from some MySQL fields. Since the data contains many unicode characters, I tried to get the data as a series of ASCII...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.