douyue3800 2011-06-27 15:02
浏览 200
已采纳

从mysql表创建一个数组

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;
  • 写回答

4条回答 默认 最新

  • dtlygweb2017 2011-06-27 19:08
    关注

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 找人不需要人工智能回答的gamit解算后做形变分析
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错