Jump to content

Archived

This topic is now archived and is closed to further replies.

sdaniels

getting values from array

Recommended Posts

I have the following code:

$result = mysql_db_query($database, "SELECT username FROM useronline") or die("Failed!");
while ($here = mysql_fetch_array($result, )) {
echo $here . ' ';


when i echo $here, it simply shows 'array'  instead of each username

what am i doing wrong?

Share this post


Link to post
Share on other sites
[quote]
Description
array mysql_fetch_array ( resource result [, int result_type] )

Returns an array that corresponds to the fetched row and moves the internal data pointer ahead.

Via the PHP Manual
[/quote]

What does this mean?

lets say you have an array:
[key1]value1
[key2]value2
[key3]value3

Every array has a 'pointer', telling specific functions 'where' to look in an array.
[key1]value1 <---
[key2]value2
[key3]value3

theifore, the currently 'pointed to' key in the array is 'key1'.

What does this mean for fetching MYSQL results?

mysql_fetch_array returns the currently pointed-to row of returned results, and moves the pointer ahead 1.
This means that $here is an array of all collums in the returnd row. $here['username'] would reffer to the collum 'username' in the SQL database. (unless you are only using numerical indexes, but im not gonan go that deep here)

So by doing:
while ($here = mysql_fetch_array($result, ))
You are esentally saying "Loop throgh each returnd row, and set $here to the values of the collums"

Check the PHP Manual for what may be better descriptions of these concepts, and for more information.

Share this post


Link to post
Share on other sites
Right thats what im trying to do.. iv changed the code to:

$query = "SELECT username FROM useronline";
$result = mysql_query($query);
while ($here = mysql_fetch_array($result)) {
echo $here . ' ';

but it sill echos 'array' for every value in the useronline field. I need it to echo the value in the table...

Share this post


Link to post
Share on other sites
ok... lets try this again...

mysql_fetch_array() takes a resource (returnd from mysql_query()) and returns an array.

The resource is much like an array for the purposes of mysql_fetch_array(). It contains all of the rows that where returnd by the querys, and the collum names and values for each of those rows.

mysql_fetch_array() returns the row currently pointed to in an array like:
$whatever = mysql_fetch_array($result);
//$whatever['collumname'] = 'collumvalue';
and moves the pointer forward. so if you know you are only going to ever get 2 rows,
$row = mysql_fetch_array($result);
echo $row['username'];
$row = mysql_fetch_array($result);
echo $row['username'];

would echo the username from the first retrived row, then the secont retrived row.

This is because you retrived 2 rows, asked for a row, output the row, moved the pointer forward, asked for the next row, output that row.

So within the while() loop structure, you are telling it
"Gimme a row, do { whatever }, gimme the next row, do { whatever } ext. untill there are no more rows, then stop looping"

So, you are populating $here with the values of the collums of the row last returnd by mysql_fetch_array(). (as key (collumname) => value (collumvalue) )

So, in an assoc array, how would you reffer to the collum 'useronline' when you know the key, and want the value?

Share this post


Link to post
Share on other sites
are you trying to be a smart ass or help me out? ill ask someone else, i can do without the condesending attitude. thanks

Share this post


Link to post
Share on other sites
No, im trying to explain the underlying concept so you can figure it out for yourself.

Please excuse me if i seem to come off with an attitude.

Share this post


Link to post
Share on other sites
Ok, sorry my wife is drving me nuts and making me frusterated...

anyhow so your saying that I have to something like this:?

$query = "SELECT username FROM useronline";
$result = mysql_query($query);
while ($here = mysql_fetch_array($result['username'])) {
echo $here . ' ';

Share this post


Link to post
Share on other sites
No.

mysql_fetch_array($result) returns an array.

That array is assigned to $here.

The array is in this format:

echo $here['sql_result_collum_name']; // echo the value of the collum named 'sql_result_collum_name' in the row last returnd by mysql_fetch_array()


You see, mysql_fetch_array() returns a row of results in an array.
mysql_fetch_array() then moves the pointer forward, so next time you call it, the next row returnd from the query tied to the variable you pass to it (in this case, $result) will be returnd.

Share this post


Link to post
Share on other sites
ok iv tried this:

$query = "SELECT username FROM useronline";
$result = mysql_query($query);
while ($here = mysql_fetch_array($result)) {
$name = $here['username'];
echo $name . ' ';

but that doesnt work either....

Share this post


Link to post
Share on other sites
JoeHaley has pratically given you the answer in reply #4. Have you worked with arrays before?

If you're still unsure of the answer, do this to see what the array "$here" contains:
[code]<?php
while ($here = mysql_fetch_assoc($result))
  echo '<pre>' . print_r($here,true) . '</pre>';
?>[/code]

Look at the output and you should see what you need to do to get the value.

Ken

Share this post


Link to post
Share on other sites
ok i lied that did work...

I think i got my mind around it now. Thanks for your help. sorry for getting snappy..

I have another question for you if your up for it.

$query = "SELECT username FROM useronline";
$result = mysql_query($query);
while ($here = mysql_fetch_array($result)) {
$name = $here['username'];
echo $name . ' ';

the username column is going to have a ton of dupliacte entrys... I want the query to just echo the ones that are uneiqe...

should i do something like $querey = "SELECT username FROM useronline GROUP BY useronline"

or will that not work?

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.