簡體   English   中英

PHP 數組不在 foreach 循環外打印

[英]PHP array is not printing outside foreach loop

我正在處理這個腳本的主要功能是從數據庫中獲取一個帶有逗號的數字,並在 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);

您還應該考慮使用准備好的語句來提高站點的安全性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM