php - PHP数组不在foreach循环之外打印
问题描述
我正在处理这个脚本的主要功能是从数据库中用逗号获取一个数字,并在 curl 中使用它来发送带有 api 的短信。
if(isset($_POST['getnumber']))
{
$matchid2 = $_POST['matchid2'];
//connect db
require_once __DIR__ . '/../db_connect.php';
$db = new DB_CONNECT();
$con = $db->connect();
$results = $con->query("SELECT DISTINCT ur.mobile FROM matchmst mst INNER JOIN matchdetail dtl on mst.matchid=dtl.matchid INNER JOIN user ur on ur.userid=dtl.userid WHERE mst.matchid = '$matchid2'");
// look through query
$datas = array();
while($row = $results->fetch_assoc()){
$datas[] = $row;
}
foreach ($datas as $data) {
$mobiles = $data['mobile'].",";
}
}
当我试图在 html 中回显这一点时,我只从数组中获取一个值,即 84*******4,并且为空白,但我想在 html 中打印该 nubers 的完整行
<div class="form-group">
<?php echo '<label for="msg">'.$mobiles.'</label>'; ?>
</div>
那么我该如何解决这个pelase帮助。提前致谢。
解决方案
在读取数据的同时构建字符串比在更多循环中操作它更容易......
$results = $con->query("SELECT DISTINCT ur.mobile FROM matchmst mst INNER JOIN matchdetail dtl on mst.matchid=dtl.matchid INNER JOIN user ur on ur.userid=dtl.userid WHERE mst.matchid = '$matchid2'");
// look through query
$mobiles = "";
while($row = $results->fetch_assoc()){
$mobiles .= $row['mobile'] . ",";
}
// Remove trailing , if needed
$mobiles = substr($mobiles, 0, -1);
您还应该考虑使用准备好的语句来提高站点的安全性。
推荐阅读
- html - 在 SVG 文件或标签中添加悬停样式
- javascript - 角度获取 api
- django - create_user() 缺少 1 个必需的位置参数:“密码”
- java - 如何在 itext pdf Java 中创建无边框的 pdfTemplate?并且它也延伸到下一页
- sublimetext3 - 在本地机器 sublime 上打开 URL
- r - 在 R 中提取分钟级数据
- python-3.x - tracer.run('main()') 报告名称错误
- eventkit - 如何在 macOS 应用程序中使用 Swift 中的 EventKit 获取日历权限
- c# - 无法从角度检索 C# cookie
- r - 从源代码编译 R 包时,编译器是否重要?