The Midnighter Posted May 22, 2009 Share Posted May 22, 2009 Hi there I've read over about 10 examples of people populating arrays with mysql, however not one seemed to have helped me. I am trying to simply populate a array. $query = "SELECT * FROM `users`;"; $result = mysql_query($query); $userDB = array(); while ($row = mysql_fetch_row($result)) { $user = $row['Username']; $pass = $row['Password']; $userDB[] = ($user => $pass); } Does not work, I've tried variations but I don't see a need to post that here. Let me know if I can provide any more details, thank you. Quote Link to comment https://forums.phpfreaks.com/topic/159304-solved-populating-an-array-with-mysql/ Share on other sites More sharing options...
.josh Posted May 22, 2009 Share Posted May 22, 2009 is that your entire code? Quote Link to comment https://forums.phpfreaks.com/topic/159304-solved-populating-an-array-with-mysql/#findComment-840195 Share on other sites More sharing options...
iceblox Posted May 22, 2009 Share Posted May 22, 2009 I was looking for help on the same thing the other day! Not sure it it will work but give this a try; $query = "SELECT * FROM users"; $result = mysql_query($query); if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_array($result)) $arr[$row['Username']] = $row['Password']; } else { echo 'No rows found!'; } Phil Quote Link to comment https://forums.phpfreaks.com/topic/159304-solved-populating-an-array-with-mysql/#findComment-840198 Share on other sites More sharing options...
The Midnighter Posted May 22, 2009 Author Share Posted May 22, 2009 Hi Phil, I understand your code until you throw in $arr, where did you get that value from? Quote Link to comment https://forums.phpfreaks.com/topic/159304-solved-populating-an-array-with-mysql/#findComment-840199 Share on other sites More sharing options...
iceblox Posted May 22, 2009 Share Posted May 22, 2009 Oops sorry missed a line!! $arr = array(); $query = "SELECT * FROM users"; $result = mysql_query($query); if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_array($result)) $arr[$row['Username']] = $row['Password']; } else { echo 'No rows found!'; } Phil Quote Link to comment https://forums.phpfreaks.com/topic/159304-solved-populating-an-array-with-mysql/#findComment-840202 Share on other sites More sharing options...
The Midnighter Posted May 22, 2009 Author Share Posted May 22, 2009 Hi again Phil. Thanks for the repost, that's what I thought you meant. So my code now looks like: $query = "SELECT * FROM `users`;"; $result = mysql_query($query); $userDB = array(); while ($row = mysql_fetch_row($result)) { $userDB[$row['Username']] = $row['Password']; } It's still failing however... There is only one user in my Database, it looks like this: ID Username Password 1 blah password Quote Link to comment https://forums.phpfreaks.com/topic/159304-solved-populating-an-array-with-mysql/#findComment-840205 Share on other sites More sharing options...
iceblox Posted May 22, 2009 Share Posted May 22, 2009 Hmm, Try and changing this line; while ($row = mysql_fetch_row($result)) to while ($row = mysql_fetch_array($result)) Did the code I sent you not work? Phil Quote Link to comment https://forums.phpfreaks.com/topic/159304-solved-populating-an-array-with-mysql/#findComment-840208 Share on other sites More sharing options...
The Midnighter Posted May 22, 2009 Author Share Posted May 22, 2009 Ah! Changing it to mysql_fetch_array solved the problem, and I understand how this works, beautifuly. Thank you Phil, I'd add to your reputation if I could figure out how =) Quote Link to comment https://forums.phpfreaks.com/topic/159304-solved-populating-an-array-with-mysql/#findComment-840209 Share on other sites More sharing options...
iceblox Posted May 22, 2009 Share Posted May 22, 2009 No worries, glad I could help! Don't forget to change the post to solved Phil Quote Link to comment https://forums.phpfreaks.com/topic/159304-solved-populating-an-array-with-mysql/#findComment-840214 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.