Array question

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

    Array question

    Hello all,

    I have a mysql select query and got it in an array

    $query = "select persid from reactie";

    $result = mysql_query($qu ery);
    $query_data = mysql_fetch_arr ay($result);// Haal de gegevens uit de
    tabel

    NOW I WANT TO ECHO I"T:

    echo "$query_dat a[8]";

    But only the query_date[0] is valid. What am i doing wrong? there are many
    more values in the table. I want to make an array with all the values from
    the field "persid" in the database.

    Irlan





  • Janwillem Borleffs

    #2
    Re: Array question

    Irlan agous wrote:[color=blue]
    > But only the query_date[0] is valid. What am i doing wrong? there are
    > many more values in the table. I want to make an array with all the
    > values from the field "persid" in the database.
    >[/color]

    Although the result might return many rows, a single row is what you are
    getting with your code.

    If you want to populate an array with all retrieved values, you should do
    someting like:

    while ($query_data = mysql_fetch_arr ay($result)) {
    $array[] = $query_data[0];
    }

    JW



    Comment

    • Jacob Atzen

      #3
      Re: Array question

      On 2005-04-13, Irlan agous <irlan345@msn.c om> wrote:[color=blue]
      > $query = "select persid from reactie";
      >
      > $result = mysql_query($qu ery);
      > $query_data = mysql_fetch_arr ay($result);// Haal de gegevens uit de
      > tabel
      >
      > NOW I WANT TO ECHO I"T:
      >
      > echo "$query_dat a[8]";[/color]

      Every call to mysql_fetch_arr ay() will return a row from your query
      result. The normal way to get all the data is to loop like this:

      while($row = mysql_fetch_arr ay($result)) {
      // Do something with $row
      }

      --
      Cheers
      - Jacob Atzen

      Comment

      • Irlan agous

        #4
        Re: Array question

        Thanks that worked!

        Now i get a list
        of all the persid's like 2,2,3,4,4,4,5. I want to select the maximun
        existing values, the second maximum etc. For example in this care number 4
        had the highest rank(with 3 times), second is 2 (with 2 times) etc. But how
        can i save distinct numbers and its amount to a variable? becouse then im
        able to check which one is the maximum etc, thanks for the help
        [color=blue][color=green]
        >> But only the query_date[0] is valid. What am i doing wrong? there are
        >> many more values in the table. I want to make an array with all the
        >> values from the field "persid" in the database.
        >>[/color]
        >
        > Although the result might return many rows, a single row is what you are
        > getting with your code.
        >
        > If you want to populate an array with all retrieved values, you should do
        > someting like:
        >
        > while ($query_data = mysql_fetch_arr ay($result)) {
        > $array[] = $query_data[0];
        > }
        >
        > JW
        >
        >
        >[/color]


        Comment

        • Ewoud Dronkert

          #5
          Re: Array question

          On Wed, 13 Apr 2005 23:00:21 +0200, Irlan agous wrote:[color=blue]
          > of all the persid's like 2,2,3,4,4,4,5. I want to select the maximun
          > existing values, the second maximum etc. For example in this care number 4
          > had the highest rank(with 3 times), second is 2 (with 2 times) etc. But how[/color]




          --
          Firefox Web Browser - Rediscover the web - http://getffox.com/
          Thunderbird E-mail and Newsgroups - http://gettbird.com/

          Comment

          • Irlan agous

            #6
            Re: Array question

            ell i tried, and i came to the next step, i got this script working now

            $query = "SELECT persid, count(logid) from reactie GROUP BY persid order by
            persid";
            $result = mysql_query($qu ery);
            $query_data = mysql_fetch_arr ay($result);
            $total_num_user = $query_data[0];

            while($query_da ta = mysql_fetch_arr ay($result)){
            $tlogid = $query_data["count(logi d)"];
            $persid = $query_data["persid"];

            echo "$persid =";

            echo "$tlogid<br >";

            }

            shown as:
            persid - count(logid)
            16 =1
            18 =2
            19 =1
            20 =2
            21 =5

            now how can i write the scrip that i can select the maximum value of
            count(logid) ??

            Irlan
            [color=blue][color=green]
            >>
            >> $result = mysql_query($qu ery);
            >> $query_data = mysql_fetch_arr ay($result);// Haal de gegevens uit
            >> de
            >> tabel
            >>
            >> NOW I WANT TO ECHO I"T:
            >>
            >> echo "$query_dat a[8]";[/color]
            >
            > Every call to mysql_fetch_arr ay() will return a row from your query
            > result. The normal way to get all the data is to loop like this:
            >
            > while($row = mysql_fetch_arr ay($result)) {
            > // Do something with $row
            > }
            >
            > --
            > Cheers
            > - Jacob Atzen[/color]


            Comment

            Working...