Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP passing array to PDO bindParam [duplicate]

Tags:

php

mysql

pdo

PDO param:

$cto=new PDO("mysql:host=$servername;dbname=$dbname",$username,$password);

Interacting with data base

$csql=$cto->prepare("INSERT INTO `users`(`username`, `password`, `class`, `is_on`, `time_log`, `IP`)
        VALUES (:name,:pass,:class,0,0,'0')");
        $pr=[
          ':name' => $_POST['username'],
          ":pass" => $_POST['password'],
          ":class" => $_POST["class"],
        ];
        $csql->execute($pr);
        $cto=null;

My question is ,i am currently using array $pr in execute,could i pass this array with using bindParam

$csql->bindParam($pr);
$csql->exec();

Thank you for your time.

like image 821
AwwYsss Avatar asked Sep 15 '25 22:09

AwwYsss


1 Answers

You can use bindParam() in single line by this way:

$csql=$cto->prepare("INSERT INTO `users`(`username`, `password`, `class`, `is_on`, `time_log`, `IP`)
VALUES (:name,:pass,:class,0,0,'0')");

//Looping for all values into array...
foreach ($pr as $key => &$val) {
    $csql->bindParam($key, $val);
}
$csql->execute();

Hope this will help you!

like image 173
AddWeb Solution Pvt Ltd Avatar answered Sep 17 '25 20:09

AddWeb Solution Pvt Ltd