Vince889 Posted January 27, 2010 Share Posted January 27, 2010 http://i49.tinypic.com/2cqhg7k.png this is a phpmyadmin screenshot. imagine that all of those are filled with data. what sql statement could i write to grab all data from ONLY the 'lastname' COLUMN? This is what i had, but obviously, it doesn't work. It only retrieves the LAST result, because php just overwrite the $lastnameList array, opposed to adding to it. I am probably looking at this from a really complicated perspective and there is probably some cool SQL statement to do it easily. Help guys!!! $query = mysql_query("SELECT * FROM names"); while($namess = mysql_fetch_array($query)) { foreach ($names as $lastnameList) { $lastnameList = array(); array_push($lastnameList, $names['name']); } } Quote Link to comment Share on other sites More sharing options...
Vince889 Posted January 27, 2010 Author Share Posted January 27, 2010 For some reason I am missing an 'Edit' button? I can't find it anyways. Can one of the mods assist with that? Anywho, I wanted to edit the code I had posted. The $namess variable should be $names. It is a typo and does not contribute to the error. Quote Link to comment Share on other sites More sharing options...
KrisNz Posted January 27, 2010 Share Posted January 27, 2010 There's a few issues there, In the loop you reset $lastnameList on each iteration (that's why you only got the last result, however you don't need that loop at all) and array_push is only useful if you want to add more than 1 variable to an array as the function takes multiple arguments. Try this instead... $query = mysql_query('SELECT `lastname` FROM `names`') or trigger_error(mysql_error(),E_USER_ERROR) ; $lastnameslist = array(); while ($namesArray = mysql_fetch_assoc($query)) { $lastnameslist[] = $namesArray['lastname']; } print_r($lastnameslist); Quote Link to comment Share on other sites More sharing options...
Vince889 Posted January 27, 2010 Author Share Posted January 27, 2010 Amazing work. Thanks man! 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.