i have a database table with fields mobno1 and mobno2, first i would like store all the values of field mobno1 into var1 and field mobno2 into var2 by using comma seperator and pdo. my code is as follows.

$pdo = Database::connect();
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = "SELECT mobno1,mobno2 FROM custreg";
        $q = $pdo->prepare($sql);
        $q->execute();
        $data = $q->fetch(PDO::FETCH_ASSOC);
        $mobile_number1=$data['mobno1'];
                $mobile_number2=$data['mobno2'];
                Database::disconnect();
$message = urlencode($tempmsg);

// Prepare data for POST request
$data = "user=".$cpuser."&password=".$cppass."&mobiles=".$mobile_number1.",".$mobile_number2."&sms=".$message."&senderid=".$cpsender;

// Send the POST request with cURL
$ch = curl_init(''); //note https for SSL
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch); //This is the result from Textlocal
curl_close($ch);               

        Database::disconnect();

i hope someone will resolve this issue, thanks in advance.

Dani AI

Generated

Your snippet only fetched a single row, which is why you saw just one pair of numbers. already flagged the undefined $tempmsg, and correctly suggested fetchAll() / array_column() (the latter needs PHP 5.5+). pointed out you can also do this in SQL (server-side aggregation). For PHP 5.4 a small loop that collects each column into arrays, sanitizes and then implode()s them is the simplest and most portable approach.

Example: collect, clean, dedupe and implode into two comma lists.

$db = Database::connect();
$stmt = $db->prepare('SELECT mobno1,mobno2 FROM custreg');
$stmt->execute();

$lists = ['mobno1'=>[], 'mobno2'=>[]];

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    foreach (['mobno1','mobno2'] as $col) {
        $val = trim($row[$col]);
        if ($val === '') continue;
        // keep leading + if present, drop other non-digits
        $clean = preg_replace('/[^\d+]/', '', $val);
        $lists[$col][] = $clean;
    }
}

$mobno1 = implode(',', array_unique($lists['mobno1']));
$mobno2 = implode(',', array_unique($lists['mobno2']));

Database::disconnect();

Notes and gotchas:

  • Use http_build_query() for the POST body (it handles encoding) instead of hand-building an &-joined string; that also avoids the forum-escaped & problem.
  • If the provider limits recipients per request, split with array_chunk() and send several requests.
  • Normalize phone formats (decide on international format) before sending so numbers are accepted by the gateway. The simple preg_replace() above keeps a leading + and removes other non-digits; adjust if you need to preserve leading zeros.
  • Remove the duplicate Database::disconnect() in your original snippet and ensure the message variable is actually defined (fix the $tempmsg usage).

This approach answers your requirement to build two comma-separated strings for mobno1 and mobno2 across all rows (no WHERE clause), and is compatible with PHP 5.4 as requested.

Recommended Answers

All 6 Replies

Where does it stop? What is the error?

The query:

$sql = "SELECT mobno1,mobno2 FROM custreg";

presumably returns a set of rows, but

$data = $q->fetch(PDO::FETCH_ASSOC);

returns only one row. Is that what you aimed for?

Note, you have a line of code saying:

$message = urlencode($tempmsg);

But where is teh $tempmsg being defined?

I don't understand: you're talking about the data you want to save into the custreg table? If yes, where are the form, the insert query and the table definition?

Member Avatar for Member #120589

i would like store all the values of field mobno1 into var1 and field mobno2 into var2

What do you mean? Get mobno1 for every record and place it into a comma separated string ($var1)? Likewise for mobno2?

Do a fetchAll(PDO::FETCH_ASSOC) and placve the data into $records.

If you have php5.5.0 you can use array_column()...

$mobno1 = implode(',',array_column($records,'mobno1'));
$mobno2 = implode(',',array_column($records,'mobno2'));

But unclear if this is what you want...

yes diafol you are right i would like to get mobno1 for every record and place it into a comma separated string ($var1)? Likewise for mobno2
my PHP version is 5.4.34 and not supporting array_column function any alternate solutions

Why not use:

SELECT GROUP_CONCAT(mobno1) AS mobno1, GROUP_CONCAT(mobno2) AS mobno2 FROM custreg

following is the working example with WHERE clause

        $pdo = Database::connect();
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = "SELECT * FROM custreg where reg_no = ?";
        $q = $pdo->prepare($sql);
        $q->execute(array($cust_id));
        $data = $q->fetch(PDO::FETCH_ASSOC);
        $mobno1=$data['mobno1'];
                $mobno2=$data['mobno2'];
                Database::disconnect();

$message="Dear"." ".$cust_name." "."Thank you for payment of Installment No-".trim($install_no)." "."of Rs.".trim($amount)." "."from"." "."XYZ ENTERPRISES";
$message = urlencode($message);

// Prepare data for POST request
$data = "user=".$cpuser."&password=".$cppass."&mobiles=".$mobno1.",".$mobno2."&sms=".$message."&senderid=".$cpsender;
// Send the POST request with cURL
$ch = curl_init('http://localhost/sendsms.jsp'); //note https for SSL
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch); //This is the result from Textlocal
curl_close($ch);
}   

but i want to read the two fields mobno1 and mobno2 without WHERE clause

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.