note 127621 added to class.mysqli-stmt

From: Date: Tue, 13 Sep 2022 11:39:52 +0000
Subject: note 127621 added to class.mysqli-stmt
Groups: php.notes 
Request: Send a blank email to php-notes+get-219564@lists.php.net to get a copy of this message
<?php include_once "Database.php"; include_once "DatabaseObject.php"; class Artikel implements DatabaseObject { private int $id; private string $name; private string $description; private float $price; public function __construct(string $name, string $description, float $price, int $id = 0){ $this->id = $id; $this->name = $name; $this->description = $description; $this->price = $price; } public function create(){ $name = $this->getName(); $description = $this->getDescription(); $price = $this->getPrice(); $mysqli = Database::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 "Datenbankfehler"; } $this->id = $mysqli->insert_id; $stmt->close(); } } public function update(){ $name = $this->getName(); $description = $this->getDescription(); $price = $this->getPrice(); $id = $this->getId(); $mysqli = Database::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 "Datenbankfehler"; } } } public static function get($id){ $article = null; $mysqli = Database::connect(); $sql = "SELECT * FROM articles WHERE id=?"; if ($stmt = $mysqli->prepare($sql)) { $stmt->bind_param("i", $id); $stmt->execute(); $result = $stmt->get_result(); if ($a = $result->fetch_assoc()) { $article = new Artikel($a['name'], $a['description'], $a['price'], $a['id']); } $stmt->close(); } return $article; } public static function getAll(){ $articles = array(); $mysqli = Database::connect(); $sql = "SELECT * FROM articles"; if ($stmt = $mysqli->prepare($sql)) { $stmt->execute(); $result = $stmt->get_result(); while ($article = $result->fetch_assoc()) { $articles[] = new Artikel($article['name'], $article['description'], $article['price'], $article['id']); } $stmt->close(); } return $articles; } public static function delete($id){ $mysqli = Database::connect(); $sql = "DELETE * FROM articles WHERE id=?"; if ($stmt = $mysqli->prepare($sql)) { $stmt->bind_param("i", $id); $stmt->execute(); $stmt->close(); } } //GETTERS /** * @return int */ public function getId(): int { return $this->id; } /** * @param int $id */ public function setId(int $id): void { $this->id = $id; } /** * @return string */ public function getName(): string { return $this->name; } /** * @param string $name */ public function setName(string $name): void { $this->name = $name; } /** * @return string */ public function getDescription(): string { return $this->description; } /** * @param string $description */ public function setDescription(string $description): void { $this->description = $description; } /** * @return float */ public function getPrice(): float { return $this->price; } /** * @param float $price */ public function setPrice ---- Server IP: 45.112.84.4 Probable Submitter: 185.85.3.173 (proxied: 178.191.114.136) ---- Manual Page -- https://php.net/manual/en/class.mysqli-stmt.php Edit -- https://main.php.net/note/edit/127621 Del: integrated -- https://main.php.net/note/delete/127621/integrated Del: useless -- https://main.php.net/note/delete/127621/useless Del: bad code -- https://main.php.net/note/delete/127621/bad+code Del: spam -- https://main.php.net/note/delete/127621/spam Del: non-english -- https://main.php.net/note/delete/127621/non-english Del: in docs -- https://main.php.net/note/delete/127621/in+docs Del: other reasons-- https://main.php.net/note/delete/127621 Reject -- https://main.php.net/note/reject/127621 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#219564) next »