Browse Source

Use of existing mysqli connection and Exception is now default emode

Added ability to pick-up an existing mysqli connection.
Also default error handler changed to Exception from USER_ERROR
dev
colshrapnel 10 years ago
parent
commit
7b11780f83
  1. 24
      safemysql.class.php

24
safemysql.class.php

@ -26,7 +26,7 @@
* and
* ?p ("parsed") - special type placeholder, for inserting already parsed statements without any processing, to avoid double parsing.
*
* Some examples:
* Connection:
*
* $db = new SafeMySQL(); // with default settings
*
@ -38,6 +38,13 @@
* );
* $db = new SafeMySQL($opts); // with some of the default settings overwritten
*
* Alternatively, you can just pass an existing mysqli instance that will be used to run queries
* instead of creating a new connection.
* Excellent choice for migration!
*
* $db = new SafeMySQL(['mysqli' => $mysqli]);
*
* Some examples:
*
* $name = $db->getOne('SELECT name FROM table WHERE id = ?i',$_GET['id']);
* $data = $db->getInd('id','SELECT * FROM ?n WHERE id IN ?a','table', array(1,2));
@ -76,7 +83,7 @@ class SafeMySQL
'socket' => NULL,
'pconnect' => FALSE,
'charset' => 'utf8',
'errmode' => 'error', //or exception
'errmode' => 'exception', //or 'error'
'exception' => 'Exception', //Exception class name
);
@ -90,6 +97,19 @@ class SafeMySQL
$this->emode = $opt['errmode'];
$this->exname = $opt['exception'];
if (isset($opt['mysqli']))
{
if ($opt['mysqli'] instanceof mysqli)
{
$this->conn = $opt['mysqli'];
return;
} else {
$this->error("mysqli option must be valid instance of mysqli class");
}
}
if ($opt['pconnect'])
{
$opt['host'] = "p:".$opt['host'];

Loading…
Cancel
Save