note 127635 added to pdo.prepared-statements

From: Date: Thu, 15 Sep 2022 17:56:49 +0000
Subject: note 127635 added to pdo.prepared-statements
Groups: php.notes 
Request: Send a blank email to php-notes+get-219579@lists.php.net to get a copy of this message
I couldn't find any matching docs here for the CRUD operations. I hope this helps anyone: //create public function create() { $sql = "INSERT INTO name (id, field1, field2, field3) VALUES(null,?,?,?)"; $db = Database::connect(); $stmt = $db->prepare($sql); $stmt->execute(array($this->id, $this->field1, $this->field2, $this->field3)); $lastID = $db->lastInsertId(); Database::disconnect(); return $lastID; } //read public function update() { $sql = "UPDATE name SET id= ?, field1=?, field2=? , field3= ? WHERE id=?"; $db = Database::connect(); $stmt = $db->prepare($sql); $stmt->execute(array($this->id, $this->field1, $this->field2, $this->field3,)); Database::disconnect(); } //view public static function get($id) { $sql = "SELECT * FROM name WHERE id=?"; $db = Database::connect(); $stmt = $db->prepare($sql); $stmt->execute(array($id)); $item = $stmt->fetchObject('name'); Database::disconnect(); return $item !== false ? $item : null; } //view public static function getAll() { $sql = "SELECT * FROM name"; $db = Database::connect(); $stmt = $db->prepare($sql); $stmt->execute(); $items = $stmt->fetchAll(PDO::FETCH_CLASS, 'name'); Database::disconnect(); return $items; } //delete public static function delete($id) { $sql = "DELETE FROM name WHERE id=?"; $db = Database::connect(); $stmt = $db->prepare($sql); $stmt->execute(array($id)); Database::disconnect(); ---- Server IP: 45.112.84.4 Probable Submitter: 185.85.3.173 (proxied: 2001:4bb8:2cd:7355:b06c:8ea3:9dd4:a9a1) ---- Manual Page -- https://php.net/manual/en/pdo.prepared-statements.php Edit -- https://main.php.net/note/edit/127635 Del: integrated -- https://main.php.net/note/delete/127635/integrated Del: useless -- https://main.php.net/note/delete/127635/useless Del: bad code -- https://main.php.net/note/delete/127635/bad+code Del: spam -- https://main.php.net/note/delete/127635/spam Del: non-english -- https://main.php.net/note/delete/127635/non-english Del: in docs -- https://main.php.net/note/delete/127635/in+docs Del: other reasons-- https://main.php.net/note/delete/127635 Reject -- https://main.php.net/note/reject/127635 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#219579) next »