Professional Web Applications Themes

Mysql result to array - MySQL

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

  1. #1

    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


    Jaak Guest

  2. #2

    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 - [email]justinkoivi.com[/email]
    [url]http://koivi.com[/url]
    Justin Koivisto Guest

  3. #3

    Default Re: Mysql result to array


    "Justin Koivisto" <justinkoivi.com> schreef in bericht
    news:dLGdnV0pDpb1jFjeRVn-rgonvoy.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.


    Jaak Guest

Similar Threads

  1. HTTPService result to AS?s array
    By paulo fabiano in forum Macromedia Flex General Discussion
    Replies: 5
    Last Post: March 4th, 06:35 PM
  2. Only 1 row of result in array
    By Szar in forum PHP Development
    Replies: 12
    Last Post: November 9th, 05:30 PM
  3. Wierd result from hash array
    By Pacman in forum PERL Miscellaneous
    Replies: 3
    Last Post: September 11th, 10:02 PM
  4. [PHP] How to store result in array for later display?
    By P Cuce in forum PHP Development
    Replies: 0
    Last Post: September 3rd, 01:31 PM
  5. Converting a MySQL result into a complex array
    By Jwulff in forum PHP Development
    Replies: 1
    Last Post: July 22nd, 12:19 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139