sdaniels Posted July 22, 2006 Share Posted July 22, 2006 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 usernamewhat am i doing wrong? Quote Link to comment Share on other sites More sharing options...
Joe Haley Posted July 22, 2006 Share Posted July 22, 2006 [quote]Descriptionarray 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]value3Every array has a 'pointer', telling specific functions 'where' to look in an array.[key1]value1 <---[key2]value2[key3]value3theifore, 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. Quote Link to comment Share on other sites More sharing options...
sdaniels Posted July 22, 2006 Author Share Posted July 22, 2006 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... Quote Link to comment Share on other sites More sharing options...
sdaniels Posted July 22, 2006 Author Share Posted July 22, 2006 do i need to do a for each on the $here variable? Quote Link to comment Share on other sites More sharing options...
Joe Haley Posted July 22, 2006 Share Posted July 22, 2006 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? Quote Link to comment Share on other sites More sharing options...
sdaniels Posted July 22, 2006 Author Share Posted July 22, 2006 are you trying to be a smart ass or help me out? ill ask someone else, i can do without the condesending attitude. thanks Quote Link to comment Share on other sites More sharing options...
Joe Haley Posted July 22, 2006 Share Posted July 22, 2006 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. Quote Link to comment Share on other sites More sharing options...
sdaniels Posted July 22, 2006 Author Share Posted July 22, 2006 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 . ' '; Quote Link to comment Share on other sites More sharing options...
Joe Haley Posted July 22, 2006 Share Posted July 22, 2006 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. Quote Link to comment Share on other sites More sharing options...
sdaniels Posted July 22, 2006 Author Share Posted July 22, 2006 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.... Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted July 22, 2006 Share Posted July 22, 2006 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]<?phpwhile ($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 Quote Link to comment Share on other sites More sharing options...
sdaniels Posted July 22, 2006 Author Share Posted July 22, 2006 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? Quote Link to comment Share on other sites More sharing options...
Barand Posted July 22, 2006 Share Posted July 22, 2006 SELECT DISTINCT username FROM useronline Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.