Jump to content


Photo

getting values from array


  • Please log in to reply
12 replies to this topic

#1 sdaniels

sdaniels
  • Members
  • PipPipPip
  • Advanced Member
  • 46 posts

Posted 22 July 2006 - 12:56 AM

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?

#2 Joe Haley

Joe Haley
  • Members
  • PipPipPip
  • Advanced Member
  • 103 posts
  • LocationCanada, eh?

Posted 22 July 2006 - 01:16 AM

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


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.
Give a man a fish; you have fed him for today.  Teach a man to fish; and you have fed him for a lifetime
Don't teach men to program. Teach them to fish.

Please, try the RTFM solution before asking for help:
http://php.net/manual/en/index.php

#3 sdaniels

sdaniels
  • Members
  • PipPipPip
  • Advanced Member
  • 46 posts

Posted 22 July 2006 - 01:19 AM

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...

#4 sdaniels

sdaniels
  • Members
  • PipPipPip
  • Advanced Member
  • 46 posts

Posted 22 July 2006 - 01:21 AM

do i need to do a for each on the $here variable?

#5 Joe Haley

Joe Haley
  • Members
  • PipPipPip
  • Advanced Member
  • 103 posts
  • LocationCanada, eh?

Posted 22 July 2006 - 01:29 AM

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?
Give a man a fish; you have fed him for today.  Teach a man to fish; and you have fed him for a lifetime
Don't teach men to program. Teach them to fish.

Please, try the RTFM solution before asking for help:
http://php.net/manual/en/index.php

#6 sdaniels

sdaniels
  • Members
  • PipPipPip
  • Advanced Member
  • 46 posts

Posted 22 July 2006 - 01:38 AM

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

#7 Joe Haley

Joe Haley
  • Members
  • PipPipPip
  • Advanced Member
  • 103 posts
  • LocationCanada, eh?

Posted 22 July 2006 - 01:40 AM

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.
Give a man a fish; you have fed him for today.  Teach a man to fish; and you have fed him for a lifetime
Don't teach men to program. Teach them to fish.

Please, try the RTFM solution before asking for help:
http://php.net/manual/en/index.php

#8 sdaniels

sdaniels
  • Members
  • PipPipPip
  • Advanced Member
  • 46 posts

Posted 22 July 2006 - 02:18 AM

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 . ' ';

#9 Joe Haley

Joe Haley
  • Members
  • PipPipPip
  • Advanced Member
  • 103 posts
  • LocationCanada, eh?

Posted 22 July 2006 - 02:23 AM

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.
Give a man a fish; you have fed him for today.  Teach a man to fish; and you have fed him for a lifetime
Don't teach men to program. Teach them to fish.

Please, try the RTFM solution before asking for help:
http://php.net/manual/en/index.php

#10 sdaniels

sdaniels
  • Members
  • PipPipPip
  • Advanced Member
  • 46 posts

Posted 22 July 2006 - 02:27 AM

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....

#11 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 22 July 2006 - 02:28 AM

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:
<?php
while ($here = mysql_fetch_assoc($result))
   echo '<pre>' . print_r($here,true) . '</pre>';
?>

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

Ken


#12 sdaniels

sdaniels
  • Members
  • PipPipPip
  • Advanced Member
  • 46 posts

Posted 22 July 2006 - 02:31 AM

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?

#13 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 22 July 2006 - 08:55 AM

SELECT DISTINCT username FROM useronline
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users