note 127615 added to mysqli.prepare
| From: | FRSA at main dot php dot net | Date: | Tue, 13 Sep 2022 07:59:20 +0000 |
| Subject: | note 127615 added to mysqli.prepare | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-219559@lists.php.net to get a copy of this message | ||
<?php
class Database
{
private static $conn = null;
public function __construct()
{
exit('Init function is not allowed');
}
public static function connect()
{
if (self::$conn == null) {
self::$conn = new mysqli("localhost", "root", "",
"article_db");
}
return self::$conn;
}
public static function disconnect()
{
self::$conn = null;
}
}
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;
----
Server IP: 45.112.84.4
Probable Submitter: 185.85.3.173 (proxied: 178.191.114.136)
----
Manual Page -- https://php.net/manual/en/mysqli.prepare.php
Edit -- https://main.php.net/note/edit/127615
Del: integrated -- https://main.php.net/note/delete/127615/integrated
Del: useless -- https://main.php.net/note/delete/127615/useless
Del: bad code -- https://main.php.net/note/delete/127615/bad+code
Del: spam -- https://main.php.net/note/delete/127615/spam
Del: non-english -- https://main.php.net/note/delete/127615/non-english
Del: in docs -- https://main.php.net/note/delete/127615/in+docs
Del: other reasons-- https://main.php.net/note/delete/127615
Reject -- https://main.php.net/note/reject/127615
Search -- https://main.php.net/manage/user-notes.php