Daniel H
Daniel H

Reputation: 2925

Creating an array from a mysql table

I am trying to create an automated log in system with cakePHP and need a bit of help working out how to get an array of possible log ins. At the moment I have this code, which means I have to manually add in the log in details each time a new user is needed:

$this->Security->loginUsers = array(
    'user1' => 'password1',
    'user2' => 'password2'
);

I have a mysql table called 'operators' which looks like this:

**Username     Password**
 user1      password1
 user2      password2
 user3      password3

etc. and this is automatically populated from a registration form. Could someone please tell me how I would turn the table into an array like the one above so that I can use it in the cakePHP code?

Thanks for any help

Edit: This is the code I have now but it doesn't work

    $test = mysql_query("SELECT * FROM operators");

    while($row = mysql_fetch_array($test))

    {

    $array = "'".$row['username']."' => '".$row['password']."'";


    }

$this->Security->loginUsers = $array;

Upvotes: 2

Views: 109

Answers (4)

YonoRan
YonoRan

Reputation: 1728

so your using cakePHP right? Does you 'operators' table have a model? if so why not just:

$users = $this->Operator->find('list');

get a list out of the table - it should already be in an array just the way you want it.

Upvotes: 1

dar7yl
dar7yl

Reputation: 3747

Try this:

   $test = mysql_query("SELECT * FROM operators");

    $users = array();
    while($row = mysql_fetch_array($test))
    {
        $users[ $row['username'] ] = $row['password'];
    }

    $this->Security->loginUsers = $users;

Upvotes: 2

user745235
user745235

Reputation:

I think it will work:

 $test = mysql_query("SELECT * FROM operators");
 $results = array();
    while($row = mysql_fetch_array($test))

    {

    $results['username'] = $row['password'];


    }

Upvotes: 1

Lukas Knuth
Lukas Knuth

Reputation: 25755

Read the manual for PHP's MySQLi-class.

Upvotes: 1

Related Questions