Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you wanted to move over to PDO then here is a quick example of what I think your trying to achieve. A <a href="http://en.wikipedia.org/wiki/Create,_read,_update_and_delete" rel="nofollow">CreateReadUpdateDelete</a> (CRUD) class.</p> <pre><code>&lt;?php class MySQLDatabase{ public $db; function __construct($dsn, $user=null, $pass=null){ $this-&gt;dsn = $dsn; $this-&gt;user = $user; $this-&gt;pass = $pass; //Connect $this-&gt;connect(); } function connect(){ try{ $this-&gt;db = new PDO($this-&gt;dsn, $this-&gt;user, $this-&gt;pass); $this-&gt;db-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this-&gt;db-&gt;setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $this-&gt;db-&gt;setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC); }catch (Exception $e){ die('Cannot connect to databse. Details:'.$e-&gt;getMessage()); } } function Select($table, $fieldname=null, $fieldvalue=null){ $sql = "SELECT * FROM $table"; $sql .=($fieldname != null &amp;&amp; $fieldvalue != null)?" WHERE $fieldname=:id":null; $statement = $this-&gt;db-&gt;prepare($sql); if($fieldname != null &amp;&amp; $fieldvalue != null){$statement-&gt;bindParam(':id', $fieldvalue);} $statement-&gt;execute(); return $statement-&gt;fetchAll(PDO::FETCH_ASSOC); } function Insert($table, $values){ $fieldnames = array_keys($values[0]); $sql = "INSERT INTO $table"; $fields = '( ' . implode(' ,', $fieldnames) . ' )'; $bound = '(:' . implode(', :', $fieldnames) . ' )'; $sql .= $fields.' VALUES '.$bound; $statement = $this-&gt;db-&gt;prepare($sql); foreach($values as $vals){ $statement-&gt;execute($vals); } } function Update($table, $fieldname, $value, $where_key, $id){ $sql = "UPDATE `$table` SET `$fieldname`= :value WHERE `$where_key` = :id"; $statement = $this-&gt;db-&gt;prepare($sql); $statement-&gt;bindParam(':id', $id); $statement-&gt;bindParam(':value', $value); $statement-&gt;execute(); } function Delete($table, $fieldname=null, $id=null){ $sql = "DELETE FROM `$table`"; $sql .=($fieldname != null &amp;&amp; $id != null)?" WHERE $fieldname=:id":null; $statement = $this-&gt;db-&gt;prepare($sql); if($fieldname != null &amp;&amp; $id != null){$statement-&gt;bindParam(':id', $id);} $statement-&gt;execute(); } } //Sample Usage $db = new MySQLDatabase('mysql:host=localhost;dbname=test','root','password'); //Multi insert: $insert = array(array('some_col'=&gt;'This was inserted by the $db-&gt;Insert() method'), array('some_col'=&gt;'Test insert 2'), array('some_col'=&gt;'Test insert 3'), ); $db-&gt;Insert('pdo_test', $insert); //Select All $result = $db-&gt;Select('pdo_test'); /* Array ( [0] =&gt; Array ( [id] =&gt; 2 [some_col] =&gt; This was inserted by the $db-&gt;Insert() method ) [1] =&gt; Array ( [id] =&gt; 3 [some_col] =&gt; Test insert 2 ) [2] =&gt; Array ( [id] =&gt; 4 [some_col] =&gt; Test insert 3 ) ) */ //Single select $result = $db-&gt;Select('pdo_test','id','2'); /* Array ( [0] =&gt; Array ( [id] =&gt; 2 [some_col] =&gt; This was inserted by the $db-&gt;Insert() method ) ) */ /* Delete Single record*/ $db-&gt;Delete('pdo_test', 'id', '2'); /*Delete All*/ $db-&gt;Delete('pdo_test'); //Array ( ) $result = $db-&gt;Select('pdo_test'); ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload