Connecting Tech Pros Worldwide Forums | Help | Site Map

Array question

Irlan agous
Guest
 
Posts: n/a
#1: Jul 17 '05
Hello all,

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

$query = "select persid from reactie";

$result = mysql_query($query);
$query_data = mysql_fetch_array($result);// Haal de gegevens uit de
tabel

NOW I WANT TO ECHO I"T:

echo "$query_data[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
Guest
 
Posts: n/a
#2: Jul 17 '05

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_array($result)) {
$array[] = $query_data[0];
}

JW



Jacob Atzen
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Array question


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

Every call to mysql_fetch_array() 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_array($result)) {
// Do something with $row
}

--
Cheers
- Jacob Atzen
Irlan agous
Guest
 
Posts: n/a
#4: Jul 17 '05

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_array($result)) {
> $array[] = $query_data[0];
> }
>
> JW
>
>
>[/color]


Ewoud Dronkert
Guest
 
Posts: n/a
#5: Jul 17 '05

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]

http://dev.mysql.com/doc/mysql/en/counting-rows.html


--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Irlan agous
Guest
 
Posts: n/a
#6: Jul 17 '05

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($query);
$query_data = mysql_fetch_array($result);
$total_num_user = $query_data[0];

while($query_data = mysql_fetch_array($result)){
$tlogid = $query_data["count(logid)"];
$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($query);
>> $query_data = mysql_fetch_array($result);// Haal de gegevens uit
>> de
>> tabel
>>
>> NOW I WANT TO ECHO I"T:
>>
>> echo "$query_data[8]";[/color]
>
> Every call to mysql_fetch_array() 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_array($result)) {
> // Do something with $row
> }
>
> --
> Cheers
> - Jacob Atzen[/color]


Closed Thread