note 127634 added to mysqli-stmt.bind-param

From: Date: Thu, 15 Sep 2022 15:29:03 +0000
Subject: note 127634 added to mysqli-stmt.bind-param
Groups: php.notes 
Request: Send a blank email to php-notes+get-219578@lists.php.net to get a copy of this message
I know Its rather long example at 126 lines of code but I think it covers the basic CRUD methods with mysqli prepared statements quite well. ~Aayush <?php require_once "Db.php"; // Database connection class Article { private $id; private $name; private $description; private $price; /** * @param $id * @param $name * @param $description * @param $price */ public function __construct($name, $description, $price, $id=0) { $this->id = $id; $this->name = $name; $this->description = $description; $this->price = $price; } /** * @inheritDoc */ public function create() { $name = $this->getName(); $description = $this->getDescription(); $price = $this->getPrice(); $mysqli = Db::connect(); $sql = "INSERT INTO articles (name, description, price) VALUES (?,?,?)"; if ($stmt = $mysqli->prepare($sql)) { $stmt->bind_param("ssd", $name, $description, $price); $stmt->execute(); if ($stmt->affected_rows == 1) { header("Location: index.php"); } else { / echo "Database error"; } $this->id = $mysqli->insert_id; $stmt->close(); } } /* $article = new Article($name, $description, $price); $article->create();*/ /** * @inheritDoc */ public function update() { $name = $this->getName(); $description = $this->getDescription(); $price = $this->getPrice(); $id = $this->getId(); $mysqli = Db::connect(); $sql = "UPDATE articles SET name=?, description=?, price=? WHERE id=?"; if ($stmt = $mysqli->prepare($sql)) { $stmt->bind_param("ssdi", $name, $description, $price, $id); $stmt->execute(); if ($stmt->affected_rows == 1) { header("Location: index.php"); } else { echo "Database error"; } } } /*$article->setName($name); $article->setDescription($description); $article->setPrice($price); $article->update();*/ /** * @inheritDoc */ public static function get($id) { $mysqli = Db::connect(); $sql = "SELECT name, description, price FROM articles WHERE id=?"; if ($stmt = $mysqli->prepare($sql)) { $stmt->bind_param("i",$id); $stmt->execute(); $stmt->bind_result($name, $description, $price); if ($stmt->fetch()) { return new Article($name, $description, $price); } $stmt->close(); } return null; } /*$article = Article::get($_GET['id']);*/ /** * @inheritDoc */ public static function getAll() { $articles = []; $mysqli = Db::connect(); $sql = "SELECT * FROM articles"; if ($stmt = $mysqli->prepare($sql)) { $stmt->execute(); $result = $stmt->get_result(); while ($row = $result->fetch_assoc()) { $articles[] = new Article($row['name'], $row['description'], $row['price'], $row['id']); } $stmt->close(); } return $articles; } /*$allArticles = Article::getAll();*/ /** * @inheritDoc */ public static function delete($id) { $mysqli = Db::connect(); $sql = "DELETE FROM articles WHERE id=?"; if ($stmt = $mysqli->prepare($sql)) { $stmt->bind_param("i", $id); $stmt->execute(); $stmt->close(); } } /*Article::delete($_GET['id']);*/ // Getter and Setter ... ---- Server IP: 45.112.84.4 Probable Submitter: 185.85.3.173 (proxied: 89.144.207.228) ---- Manual Page -- https://php.net/manual/en/mysqli-stmt.bind-param.php Edit -- https://main.php.net/note/edit/127634 Del: integrated -- https://main.php.net/note/delete/127634/integrated Del: useless -- https://main.php.net/note/delete/127634/useless Del: bad code -- https://main.php.net/note/delete/127634/bad+code Del: spam -- https://main.php.net/note/delete/127634/spam Del: non-english -- https://main.php.net/note/delete/127634/non-english Del: in docs -- https://main.php.net/note/delete/127634/in+docs Del: other reasons-- https://main.php.net/note/delete/127634 Reject -- https://main.php.net/note/reject/127634 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#219578) next »