简体   繁体   中英

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

I'm using the Twilio API to send sms. I'm trying to change it so that I can send to a list of recipients from mysql results.

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.

You have logic in your array definition. You should define the array, and then populate it with the 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'];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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