db = new PDO( 'mysql:host=localhost;dbname=data_pdo_mysql', 'username', 'password'); $this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); } catch (PDOException $e) { throw new RestException(501, 'MySQL: ' . $e->getMessage()); } } function get ($id, $installTableOnFailure = FALSE) { $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); try { $sql = 'SELECT * FROM authors WHERE id = ' . mysql_escape_string( $id); return $this->id2int($this->db->query($sql) ->fetch()); } catch (PDOException $e) { if (! $installTableOnFailure && $e->getCode() == '42S02') { //SQLSTATE[42S02]: Base table or view not found: 1146 Table 'authors' doesn't exist $this->install(); return $this->get($id, TRUE); } throw new RestException(501, 'MySQL: ' . $e->getMessage()); } } function getAll ($installTableOnFailure = FALSE) { $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); try { $stmt = $this->db->query('SELECT * FROM authors'); return $this->id2int($stmt->fetchAll()); } catch (PDOException $e) { if (! $installTableOnFailure && $e->getCode() == '42S02') { //SQLSTATE[42S02]: Base table or view not found: 1146 Table 'authors' doesn't exist $this->install(); return $this->getAll(TRUE); } throw new RestException(501, 'MySQL: ' . $e->getMessage()); } } function insert ($rec) { $name = mysql_escape_string($rec['name']); $email = mysql_escape_string($rec['email']); $sql = "INSERT INTO authors (name, email) VALUES ('$name', '$email')"; if (! $this->db->query($sql)) return FALSE; return $this->get($this->db->lastInsertId()); } function update ($id, $rec) { $id = mysql_escape_string($id); $name = mysql_escape_string($rec['name']); $email = mysql_escape_string($rec['email']); $sql = "UPDATE authors SET name = '$name', email ='$email' WHERE id = $id"; if (! $this->db->query($sql)) return FALSE; return $this->get($id); } function delete ($id) { $r = $this->get($id); if (! $r || ! $this->db->query( 'DELETE FROM authors WHERE id = ' . mysql_escape_string($id))) return FALSE; return $r; } private function id2int ($r) { if (is_array($r)) { if (isset($r['id'])) { $r['id'] = intval($r['id']); } else { foreach ($r as &$r0) { $r0['id'] = intval($r0['id']); } } } return $r; } private function install () { $this->db->exec( "CREATE TABLE authors ( id INT AUTO_INCREMENT PRIMARY KEY , name TEXT NOT NULL , email TEXT NOT NULL );"); $this->db->exec( "INSERT INTO authors (name, email) VALUES ('Jac Wright', 'jacwright@gmail.com'); INSERT INTO authors (name, email) VALUES ('Arul Kumaran', 'arul@luracast.com'); "); } }