Jump to content

[SOLVED] Populating an array with MySQL


The Midnighter

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/159304-solved-populating-an-array-with-mysql/
Share on other sites

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

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

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

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.