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
Share      Blog   Popular

PHPize.online is a free online environment for quickly running, experimenting with and sharing PHP (including Carbon extension for DateTime) and SQL code. You can run your SQL code with PHP code that can use the same DB. For database manipulations you can use pre-defined instances of PDO ($pdo), mysqli ($mysqli) & Laravel query builder ($db)

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);
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) VALUES (:name, :email)', [':name' => $name, ':email' => $email]); } } $db = new Database(); $db->addUser('DROP TABLE users;', "richard@mail.ru"); print_r($db->getUsers());
Show:  
Copy Clear