简体   繁体   English

如何用来自MySQL的数据填充PHP数组?

[英]How do I fill a PHP array with data from MySQL?

I'm using the Twilio API to send sms. 我正在使用Twilio API发送短信。 I'm trying to change it so that I can send to a list of recipients from mysql results. 我正在尝试更改它,以便可以从mysql结果发送到收件人列表。

The example code given is: 给出的示例代码为:

$people = array(
    "+14155551212" => "First Lastname",
);

My code is: 我的代码是:

$people = array(
    while($res = mysql_fetch_array($usersphone)) {
    $people[$res['UserMobile']] = $res['UserFirstName'];
    }
);

The syntax is bad but I can't figure out where. 语法不好,但我不知道在哪里。

You can't put control structures into arrays. 您不能将控制结构放入数组中。

$people = array();
while ($res = mysql_fetch_array($usersphone)) {
    $people[$res["UserMobile"]] = $res["UserFirstName"];
};

Also, there's a ton of posts here on SO that will tell you all about not using the mysql_* functions anymore, since they're deprecated. 另外,SO上有很多帖子,它们将告诉您有关不再使用mysql_*函数的所有信息,因为它们已被弃用。

You have logic in your array definition. 您在数组定义中有逻辑。 You should define the array, and then populate it with the while. 您应该定义数组,然后用while填充它。

// define the array
$people = array();
while($res = mysql_fetch_array($usersphone)) {
    // populate key with mobile and value with name
    $people[$res['UserMobile']] = $res['UserFirstName'];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM