Hi! Could we please enable some services and cookies to improve your experience and our website?

PHPize | SQLize | SQLtest

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

A A A
Login    Share code      Blog   FAQ
Copy Format Clear
CREATE TABLE `mytable` ( `id` int(11) NOT NULL, `name` varchar(255) CHARACTER SET latin1 NOT NULL, `text` varchar(255) CHARACTER SET latin1 NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ALTER TABLE `mytable` ADD PRIMARY KEY (`id`); ALTER TABLE `mytable` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;

Stuck with a problem? Got Error? Ask AI support!

Copy Clear
Copy Format Clear
<?php class MyQuery { public $dbConn; public function __construct($dbConn) { $this->dbConn = $dbConn; } public function query($query, $params) { $stmt = $this->dbConn->prepare($query); $stmt->execute($params); return $stmt; } public function transaction(Callable $f) { try { $this->dbConn->beginTransaction(); $f($this); $this->dbConn->commit(); } catch (\Throwable $e) { $this->dbConn->rollBack(); throw $e; } } public function multiInsert($queries) { $this->transaction( function () use ($queries) { foreach ($queries as $row) { $this->query($row['query'], $row['params']); } }); } public function updateRow($queries) { $this->transaction( function () use ($queries) { $this->query($queries['query'], $queries['params']); }); } } $myQuery = new MyQuery($pdo); $insert_query[] = [ "query" => "INSERT INTO mytable (name, text) VALUES (:name, :text);", "params" => [":name" => "Another Name", ":text" => "Another Text"], ]; $insert_query[] = [ "query" => "INSERT INTO mytable (name, text) VALUES (:name, :text);", "params" => [":name" => "Another Name", ":text" => "Another Text"], ]; $myQuery->multiInsert($insert_query); $update_query = [ "query" => "UPDATE mytable SET name=:name, text=:text WHERE id=:id", "params" => [":name" => "Another Name (Updated)", ":text" => "Another Text (Updated)", ":id" => "1"], ]; $myQuery->updateRow($update_query);
Copy Clear