note 129383 added to mysqli-stmt.bind-param

From: Date: Sun, 07 Apr 2024 23:12:21 +0000
Subject: note 129383 added to mysqli-stmt.bind-param
Groups: php.notes 
Request: Send a blank email to php-notes+get-221278@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 = 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/129383 Del: integrated -- https://main.php.net/note/delete/129383/integrated Del: useless -- https://main.php.net/note/delete/129383/useless Del: bad code -- https://main.php.net/note/delete/129383/bad+code Del: spam -- https://main.php.net/note/delete/129383/spam Del: non-english -- https://main.php.net/note/delete/129383/non-english Del: in docs -- https://main.php.net/note/delete/129383/in+docs Del: other reasons-- https://main.php.net/note/delete/129383 Reject -- https://main.php.net/note/reject/129383 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#221278) next »