如何从MySQL数据创建PHP数组

时间:2015-07-22 03:29:45

标签: php mysql google-cloud-messaging

我有一个带有列的数据库" regId"在一个表用户 我想要做的是选择所有行,但只选择该列(SELECT regId FROM users)

然而,在PHP中,我希望将其作为

数组("第一行ID","第二行ID"等等)

我正在使用Google云消息传递构建应用,并且需要注册ID,例如"数组("某些","其他内容")

这是我的代码

<?php
session_start();
require_once("global.php");



$qry = "SELECT `regId` FROM `users` WHERE regId IS NOT NULL"; //Here's where the IDs are
$fetchQry = mysqli_query($connection, $qry);

// Replace with the real server API key from Google APIs
$apiKey = "/////";

// Replace with the real client registration IDs




$registrationIDs = array("","","",""); //heres how they need the IDs

// Message to be sent
$message = "somethinnnnnn";

// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';

$fields = array(
    'registration_ids' => $registrationIDs,
    'data' => array( "message" => $message ),
);
$headers = array(
    'Authorization: key=' . $apiKey,
    'Content-Type: application/json'
);

// Open connection
$ch = curl_init();

// Set the URL, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $url);
curl_setopt( $ch, CURLOPT_POST, true);
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields));

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// curl_setopt($ch, CURLOPT_POST, true);
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields));

// Execute post
$result = curl_exec($ch);

// Close connection
curl_close($ch);
echo $result;
//print_r($result);
//var_dump($result);
?>

我检查了类似的问题,他们都使用了一个while循环,例如

$rows = [];
while($row = mysqli_fetch_array($result))
{
    $rows[] = $row;
}

但是,如果我将$ registrationIDs变量设置为$ rows或array($ rows),则它不起作用! (甚至回应他们并没有显示数据)

2 个答案:

答案 0 :(得分:1)

我最终还是把它弄出来了。感谢Create PHP array from MySQL column

这就是我的所作所为:

$column = array();

while($row = mysqli_fetch_array($result)){
    $column[] = $row['regId'];
//Edited - added semicolon at the End of line.1st and 4th(prev) line

}

我之前做过尝试过但是它最初是mysql而不是mysqli !! 然后我还将$ registrationIDs变量设置为$ column

答案 1 :(得分:0)

试试这个:

NSArray *words = [texfield.text componentsSeparatedByString:@" "];
int index = [words indexOfObject:@"string"];

看看它是否可行。