From 7b11780f834afb21b4139a88fde154322f5b6373 Mon Sep 17 00:00:00 2001
From: colshrapnel
Date: Fri, 8 May 2015 08:57:12 +0300
Subject: [PATCH] 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
---
safemysql.class.php | 24 ++++++++++++++++++++++--
1 file changed, 22 insertions(+), 2 deletions(-)
diff --git a/safemysql.class.php b/safemysql.class.php
index 7c2df96..44623aa 100644
--- a/safemysql.class.php
+++ b/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'];