Hi i have a array which is populated from a mysql query, It selects values from 3 columns in the table. I was first wondering how to retrieve say the id column value from the array. When i do a print_r(array_values($members)); line it comes out with this: Array ( [0] => Array ( [0] => 2 [id] => 2 [1] => thommo [username] => thommo [2] => abc [password] => abc ) ). I was wondering why it spits out like this when all the examples i have seen come out without array then another array.
So i was wondering how i can retrieve the id from the array and also why my array is printing out the way it is.
PHP Arrays
Moderator: General Moderators
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
Re: PHP Arrays
try using the form:
Does that answer your question? Or did I misinterpret?
Code: Select all
$result = MySQL_query("SELECT id, row1, row2 FROM sometable...") or die(MySQL_error());
while($row = MySQL_fetch_assoc($result)){
// do stuff per row here, like
echo $row['id'];
}Re: PHP Arrays
yeah that does help, but the query i have is in an query page where is performs the query and brings back one result which is $members = get_login($username);. After this i then want to go through the completed queries array $member and pick out the id but it doesn't allow me because the array when printed looks like this: Array ( [0] => Array ( [0] => 2 [id] => 2 [1] => thommo [username] => thommo [2] => jt [password] => jt ) ) and when i use the line to get the id it comes up blank.
-
nowaydown1
- Forum Contributor
- Posts: 169
- Joined: Sun Apr 27, 2008 1:22 am
Re: PHP Arrays
I think something else is going on if the array you are getting back from that function is indeed multi-dimensional. Kieran suggested the use of mysql_fetch_assoc because it looks like your array is giving you both numeric indexes and associative indexes. This is typical of a mysql_fetch_array call with the result type of MYSQL_BOTH specified (which is default). I find it a little less confusing to use MYSQL_ASSOC, or switch your mysql_fetch_array call to mysql_fetch_assoc as Kieran suggested. Up to you.
I would guess the additional zero based index is because you're shoving every row onto a new index. Normally when that happens your code is something like:
At any rate, if you just wanted to access the ID parameter in your multi-dimensional array just use:
I would guess the additional zero based index is because you're shoving every row onto a new index. Normally when that happens your code is something like:
Code: Select all
while($row = mysql_fetch_array($result)) {
$newArray[] = $row;
}
Code: Select all
$idValue = $members[0]["id"];
Re: PHP Arrays
Looking at the array you have posted;
I am not sure what the layout is of your database table is, but to me this looks like the result of mysql_fetch_array($result, MYSQL_BOTH), and my guess is that you have the following fields; id, username, and password. The first part of the array "Array ( [0] => Array (..." is the result of the returned row. Since you are looking to return only one row in the result, you may want to look into using something like this:
Doing it this way will result in:
allowing you to use either $members[0] or $members['id'] to get the ID, $members[1] or $members['username'] to get the username, etc...
The key to this is the PHP list function. If you do not use the list function and just use this:
You would then need to reference $members[0][0] or $members[0]['id'] to get the ID and $members[0][1] or $members[0]['username'] to get the username, etc... Where $members[0] is the first and only row result.
using mysql_fetch_assoc with the list function would result in the following array:
This would allow you to use $members['id'], $members['username'] or $members['password'] to reference any of the 3 fields.
Hope this explanation was helpful.
Code: Select all
Array ( [0] => Array ( [0] => 2 [id] => 2 [1] => thommo [username] => thommo [2] => abc [password] => abc ) )Code: Select all
$result = mysql_query("SELECT * FROM user_data WHERE id = $id LIMIT 1");
if ($results) {
//Personally I prefer mysql_fetch_assoc which just returns the associative array portion of the result set like this
//list($members) = mysql_fetch_assoc($result);
//but there may be an advantage to using the result the way it is below
list($members) = mysql_fetch_array($result, MYSQL_BOTH);
}
print_r($members);
Code: Select all
Array ( [0] => 2 [id] => 2 [1] => thommo [username] => thommo [2] => abc [password] => abc ) )The key to this is the PHP list function. If you do not use the list function and just use this:
Code: Select all
$data = mysql_fetch_array($result, MYSQL_BOTH);using mysql_fetch_assoc with the list function would result in the following array:
Code: Select all
Array ( [id] => 2 [username] => thommo [password] => abc ) )Hope this explanation was helpful.