note 129382 added to mysqli-stmt.bind-param
| From: | julio.necronomicon@gmail.com | Date: | Sun, 07 Apr 2024 22:35:47 +0000 |
| Subject: | note 129382 added to mysqli-stmt.bind-param | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-221277@lists.php.net to get a copy of this message | ||
Is my simple way for use Mysqli with params.
First: Create a table with 3 fields codigo, nome and email
Second: Writing a function to query execute
<?php
// Simple and basic connect to database (example)
$db = new Mysqli('localhost', 'root', 'admin123', 'db02');
$db->set_charset("utf8");
// Set my param to use in query
$codigo = 2;
// Usage queryExecute
$result = $this->queryExecute($db, array(
'fields'=>['codigo', 'nome', 'email'],
'params'=>['types'=>'i',
'values'=>[$codigo]],
'query' => 'SELECT * FROM teste WHERE codigo = ?'
));
echo '<pre>';
print_r($result);
echo '</pre>';
// MySqli - Simple Driver
function queryExecute($db, $data = array()) {
$stmt = $db->prepare($data['query']);
if(isset($data['params']) && count($data['params']) > 0) {
$params = $data['params']['values'];
$stmt->bind_param($data['params']['types'], ...$params);
}
$stmt->execute();
$stmt->store_result();
$fields = $data['fields'];
$stmt->bind_result(...$fields);
$collection = array();
$columns = $data['fields'];
while($stmt->fetch()) {
$row = array();
foreach($fields as $key => $value) {
$row[$columns[$key]] = $value;
}
$collection[] = $row;
}
return $collection;
}
?>
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.13.131 (proxied: 2804:7f2:2482:860f:747e:87a1:7061:9218)
----
Manual Page -- https://php.net/manual/en/mysqli-stmt.bind-param.php
Edit -- https://main.php.net/note/edit/129382
Del: integrated -- https://main.php.net/note/delete/129382/integrated
Del: useless -- https://main.php.net/note/delete/129382/useless
Del: bad code -- https://main.php.net/note/delete/129382/bad+code
Del: spam -- https://main.php.net/note/delete/129382/spam
Del: non-english -- https://main.php.net/note/delete/129382/non-english
Del: in docs -- https://main.php.net/note/delete/129382/in+docs
Del: other reasons-- https://main.php.net/note/delete/129382
Reject -- https://main.php.net/note/reject/129382
Search -- https://main.php.net/manage/user-notes.php