note 124692 added to pdo.prepared-statements
| From: | w37090 at yandex dot ru | Date: | Thu, 06 Feb 2020 18:11:09 +0000 |
| Subject: | note 124692 added to pdo.prepared-statements | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-214014@lists.php.net to get a copy of this message | ||
Insert a multidimensional array into the database through a prepared query:
We have an array to write the form:
$dataArr:
Array
(
[0] => Array
(
[0] => 2020
[1] => 23
[2] => 111111
)
[1] => Array
(
[0] => 2020
[1] => 24
[2] => 222222222
)
....
Task: prepare a request and pass through binds
$array = [];
foreach ($dataArr as $k=>$v) {
// $x = 2020, the variable is predetermined in advance, does not change the essence
$array[] = [$x, $k, $v];
}
$sql = ("INSERT INTO
table
(field,field,field) VALUES (?,?,?)");
$db->queryBindInsert($sql,$array);
public function queryBindInsert($sql,$bind) {
$stmt = $this->pdo->prepare($sql);
if(count($bind)) {
foreach($bind as $param => $value) {
$c = 1;
for ($i=0; $i<count($value); $i++) {
$stmt->bindValue($c++, $value[$i]);
}
$stmt->execute();
}
}
}
----
Server IP: 45.112.84.4
Probable Submitter: 185.85.3.195 (proxied: 37.131.203.189)
----
Manual Page -- http://php.net/manual/en/pdo.prepared-statements.php
Edit -- https://master.php.net/note/edit/124692
Del: integrated -- https://master.php.net/note/delete/124692/integrated
Del: useless -- https://master.php.net/note/delete/124692/useless
Del: bad code -- https://master.php.net/note/delete/124692/bad+code
Del: spam -- https://master.php.net/note/delete/124692/spam
Del: non-english -- https://master.php.net/note/delete/124692/non-english
Del: in docs -- https://master.php.net/note/delete/124692/in+docs
Del: other reasons-- https://master.php.net/note/delete/124692
Reject -- https://master.php.net/note/reject/124692
Search -- https://master.php.net/manage/user-notes.php