Jump to content

getting values from array


sdaniels

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?
Link to comment
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.
Link to comment
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...
Link to comment
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?
Link to comment
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 . ' ';
Link to comment
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.
Link to comment
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
Link to comment
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?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.