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('http://msg.sendsms.com/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);               

        Database::disconnect();

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

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 diafol

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.