对于我的工作,我必须用api发送多个短信,我想把数字放在mysql的数组中,如下所示:
(这些数字在我的数据库中)
我想这样:
$list_num = array(
'0601020301','0601020302','0601020303','0601020304','0601020305','0601020306','0601020307','0601020308','0601020309','0601020310',
'0601020311','0601020312','0601020313','0601020314','0601020315','0601020316','0601020317','0601020318','0601020319','0601020320',
'0601020321','0601020322','0601020323','0601020324','0601020325','0601020326','0601020327','0601020328','0601020329','0601020330'
);
我试过这个,但不起作用:
$result = mysql_query("SELECT * FROM test");
if (!$result) {
die('Requête invalide : ' . mysql_error());
}
$a = array();
while ($row = mysql_fetch_assoc($result)) // moi je fais comme ca un mysql_fetch_*
{
$a[] = $row['numero'];
foreach ($a as $val) {
$ret .= "$val";
if ($ret == end($a))
//var_dump($val);
{
//echo "'".$val."'";
}
else
{
echo "'".$val."',";
}
}
}
可能错误的方法,我之前在这里搜索, 非常感谢。
答案 0 :(得分:0)
这应该可以解决问题。您只需根据需要调整名称/参数即可。我使用了json,如果你需要做其他事情,或者解码...
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include"config.inc.php";
$mysqli = mysqli_connect("$host", "$user", "$mdp", "$db");
if (mysqli_connect_errno()) { echo "Error connecting : " . mysqli_connect_error($mysqli); }
$query = " SELECT id_test, name_test, phone_test FROM tbl_tests ";
$stmt = $mysqli->prepare($query);
$results = $stmt->execute();
$stmt->bind_result($id_test, $name_test, $phone_test);
$stmt->store_result();
if ($stmt->num_rows > 0) {
$phones = array();
$phone = array();
while($stmt->fetch()){
echo"[ $id_test -> $name_test -> $phone_test ]<br />";
$phone = "$phone_test";
array_push($phones, $phone);
}
}
else
{ echo"[ no data ]"; }
//print_r($phones);
echo json_encode($phones);
?>
输出:[&#34; 00544656347&#34;,&#34; 0065465836&#34;,&#34; 00656566745&#34;,&#34; 0068456868&#34;,&#34; 00626565064&#34; ]
编辑:请由于在PHP 5.5中不推荐使用mysql_ *(请参阅PHP doc),您应该更喜欢PPS : Prepared Parameterized Statements。这将有助于Preventing SQL injection
答案 1 :(得分:0)
我的脚本不适用于输出json,但这是一个很好的技巧。 怎么做这样:
$data = array(
array(
'number 1 from my database',
'number 2 from my database',
'number 3 from my database',
'number 4 from my database',
'number 5 from my database',
'number 6 from my database',
'number 7 from my database'
),