Mysql result to array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jaak

    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


  • frizzle

    #2
    Re: Mysql result to array


    Jaak wrote:[color=blue]
    > 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[/color]

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

    and later call it as $query['number_of_coun tings']

    Comment

    • Justin Koivisto

      #3
      Re: Mysql result to array

      Jaak wrote:[color=blue]
      > 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
      >
      >[/color]

      $q='select listid, count(*) from table1 group by listid';
      $res=mysql_quer y($q);
      if($res){
      $data=array();
      while($row=mysq l_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_ass oc, then the key name will be
      'num_found'

      HTH

      --
      Justin Koivisto, ZCE - justin@koivi.co m

      Comment

      • Justin Koivisto

        #4
        Re: Mysql result to array

        frizzle wrote:[color=blue]
        > Jaak wrote:
        >[color=green]
        >>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[/color]
        >
        >
        > you could create an alias,
        > COUNT(*) AS 'number_of_coun tings'
        >
        > and later call it as $query['number_of_coun tings'][/color]

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

        --
        Justin Koivisto, ZCE - justin@koivi.co m

        Comment

        • Jaak

          #5
          Re: Mysql result to array


          "Justin Koivisto" <justin@koivi.c om> schreef in bericht
          news:dLGdnV0pDp b1jFjeRVn-rg@onvoy.com...[color=blue]
          > Jaak wrote:[color=green]
          >> 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
          >>
          >>[/color]
          >
          > $q='select listid, count(*) from table1 group by listid';
          > $res=mysql_quer y($q);
          > if($res){
          > $data=array();
          > while($row=mysq l_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_ass oc, then the key name will be
          > 'num_found'[/color]
          Thanks Justin, it really helped me! I was confused with the 'count' thing,
          because this was not really a field in the table.


          Comment

          Working...