By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,700 Members | 1,316 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,700 IT Pros & Developers. It's quick & easy.

Array question

P: n/a
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

Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Irlan agous wrote:
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.


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

Jul 17 '05 #2

P: n/a
On 2005-04-13, Irlan agous <ir******@msn.com> wrote:
$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]";


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
Jul 17 '05 #3

P: n/a
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
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.


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

Jul 17 '05 #4

P: n/a
On Wed, 13 Apr 2005 23:00:21 +0200, Irlan agous wrote:
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


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/
Jul 17 '05 #5

P: n/a
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

$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]";


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

Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.