开发者

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;


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;


Read the manual for PHP's MySQLi-class.


I think it will work:

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

    {

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


    }


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜