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

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.