简体   繁体   中英

PHP array is not printing outside foreach loop

I am working on this script main function is to take a numbers from database with commas and use it inside curl to send a sms with 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'].",";
        }

    }

When i tried to echo this in html i am only getting one value from array ie 84*******4, and blank but i want to print complete row of that nubers in html

<div class="form-group">
        <?php echo '<label for="msg">'.$mobiles.'</label>'; ?>

</div>

So how can i solve this pelase help. Thanks in advance.

It would be easier to build the string whilst reading the data than to manipulate it in more loops...

    $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);

You should also look at using prepared statements to improve security of your site.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM