Jump to content

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

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.