Why don't you just use mysql_fetch_object and append the fetched object to the 
users array?

----- Ursprüngliche Mail ----
Von: Al-Faisal El-Dajani <[EMAIL PROTECTED]>
An: Jordan PHP User Group Mailing list <[email protected]>
Gesendet: Sonntag, den 9. März 2008, 10:47:31 Uhr
Betreff: [JoPHP] Weird PHP Problem

Hey guys, I'm facing a very weird PHP problem, and would appreciate it if 
someone would explain to me what is going on:

function getUsers()
{
$users = array();
$result = mysql_query("SELECT `id`, `name` FROM `users`");
while ($row = mysql_fetch_assoc($result)) {
$user->id = $row['id'];
$user->name = $row['name'];
$users[] = $user;

}
return $users;

}

At the end of this function the $users array contains the correct number of 
entries, but they all are the LAST row in the result set. If I print the $user 
object, it shows that it holds the correct data in every iteration. If I add 
unset($user) as the first line in the while loop it works fine. Also, if I 
define a class called user and create a new object in every iteration it works 
fine.

But why is this problem happening here? My guess is that every iteration is 
overwriting the object, but all array entries are referencing the same object. 
Is this is what is happening? if so, why is it happening like so?

-- 
Al-Faisal El-Dajani
Phone: +962-7-79737050
P.O Box: 140056
11814 Amman, Jordan







      Lesen Sie Ihre E-Mails jetzt einfach von unterwegs.
www.yahoo.de/go
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Jordan PHP Users Group" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/JoPHP
http://Jolug.org/
-~----------~----~----~----~------~----~------~--~---

Reply via email to