Jump to content

MySQL_Fetch_Array Question


Vince889

Recommended Posts

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']);


}
}

Link to comment
https://forums.phpfreaks.com/topic/189936-mysql_fetch_array-question/
Share on other sites

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);

Archived

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

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