在数组php中列出数字 [英] List number in array php
问题描述
对于我的工作,我必须发送多个短信与一个API,我想把数组从MySQL像这样:
(数字在我的数据库)
我想要这样:
$ $ $ $ $ $ $ $ $ $ $ $ list_num = array (
'0601020301','0601020302','0601020303','0601020304','0601020305','0601020306','0601020307','0601020308','0601020309','0601020310',
' 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êteinvalide:'。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。',;
$ p $可能是错误的方法,之前在这里搜索,
非常感谢。
解决方案这应该可以做到。您只需要根据需要调整名称/参数。我使用json,如果你需要做其他的事情,或者解码...
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
包含config.inc.php;
$ mysqli = mysqli_connect($ host,$ user,$ mdp,$ db);
if(mysqli_connect_errno()){echoError 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);
?>
输出:[00544656347,0065465836,00656566745,0068456868,00626565064 ]
编辑:请由于mysql_ *在PHP 5.5中不推荐使用(请参阅 PHP doc )你应该更喜欢 PPS:准备好的参数化语句。这将有助于防止SQL注入
for my work, i must to send multiples sms with an api and i want to put numbers in array from mysql like this :
(the numbers are in my database)
I want like this :
$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'
);
i have try this, but doesnt work :
$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."',";
}
}
}
Probably wrong method, i have search before here before,
Thanks a lot.
解决方案 This should do the trick. You just need to adapt names/parameters to your needs. I used json, do something else if you need, or decode...
<?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);
?>
output : ["00544656347","0065465836","00656566745","0068456868","00626565064"]
EDIT: please As mysql_* was deprecated in PHP 5.5 (please refer to PHP doc) you should prefer PPS : Prepared Parameterized Statements. This will help Preventing SQL injection
这篇关于在数组php中列出数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!