3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php class database{ // @object, The PDO object private $pdo; private $db_connected, $error; private $stmt; public function __construct($host, $dbname, $user, $pass, $char_set="utf8"){ $dsn = 'mysql:dbname='.$dbname.';host='.$host.''; try{ $this->pdo = new PDO($dsn, $user, $pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES ".$char_set)); // We can now log any exceptions on Fatal error. $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Disable emulation of prepared statements, use REAL prepared statements instead. $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $this->db_connected = true; }catch (PDOException $e){ die("Error! Could not connect to the database.<br />".$e->getMessage()); } } public function __destruct(){ $this->pdo = null; $this->db_connected = false; } public function query($query){ try{ $this->stmt = $this->pdo->prepare($query); }catch(PDOException $e){ if( _safemode_ ){ die("Error! Invalid Query.<br />".$query."<br />".$e->getMessage()); } else{ die("A fatal error occurred."); } } } public function bind($param, $value, $type = null){ if ( is_null($type) ){ switch(true){ case is_int($value): $type = PDO::PARAM_INT; break; case is_bool($value): $type = PDO::PARAM_BOOL; break; case is_null($value): $type = PDO::PARAM_NULL; break; default: $type = PDO::PARAM_STR; } } $this->stmt->bindValue($param, $value, $type); } public function execute(){ try{ $this->stmt->execute(); return true; }catch(PDOException $e){ $this->error = $e->getMessage(); return false; } } public function run(){ $this->execute(); } public function results(){ $this->execute(); return $this->stmt->fetchAll(PDO::FETCH_ASSOC); } public function single(){ $this->execute(); return $this->stmt->fetch(PDO::FETCH_ASSOC); } public function num_rows(){ return $this->stmt->rowCount(); } public function last_insert_id(){ return $this->pdo->lastInsertId(); } public function begin_transaction(){ return $this->pdo->beginTransaction(); } public function end_transaction(){ return $this->pdo->commit(); } public function cancel_transaction(){ return $this->pdo->rollBack(); } public function dump(){ return $this->stmt->debugDumpParams(); } }
Output for 5.0.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/rTv5K on line 5
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/rTv5K on line 5
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/rTv5K on line 5
Process exited with code 255.