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

PHPize Online / SQLize Online  /  SQLtest Online

A A A
Login    Share code      Blog   FAQ

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

Copy Format Clear
CREATE TABLE IF NOT EXISTS users (id INTEGER AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50) NOT NULL UNIQUE); INSERT IGNORE INTO users (name) VALUE ('BOB'); INSERT IGNORE INTO users (name) VALUE ('ALICE'); SELECT * FROM users; ALTER TABLE users ADD COLUMN email VARCHAR(256);

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

Copy Clear
Copy Format Clear
<?php class Database { public function _construct() { } public function connect() : void { } protected function executeQuery($query, $params = []) : mixed { global $pdo; try { $stmt = $pdo->prepare($query); $stmt->execute($params); return $stmt; } catch (\PDOException $e) { throw new \Exception("Database query error: " . $e->getMessage()); } } public function getUsers() : array { return array_map(fn($user) => $user['name'], $this->executeQuery('SELECT * FROM users')->fetchAll()); } public function addUser(string $name, string $email) : void { $this->executeQuery('INSERT INTO users (:name :email)', [':name' => $name, ':email' => $email]); } } $db = new Database(); $db->addUser('Richard', "richard@mail.ru") print_r($db->getUsers());
Copy Clear