[opw-web] Switch from mysqli to PDO
- From: Owen Taylor <otaylor src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [opw-web] Switch from mysqli to PDO
- Date: Mon, 10 Mar 2014 04:31:42 +0000 (UTC)
commit 32d470baf67b581cc9cd69bdae2893535c98be38
Author: Owen W. Taylor <otaylor fishsoup net>
Date: Sat Mar 8 13:41:33 2014 -0500
Switch from mysqli to PDO
classes/class_db.php | 71 ++++++++++++++++++++++++--------------------------
1 files changed, 34 insertions(+), 37 deletions(-)
---
diff --git a/classes/class_db.php b/classes/class_db.php
index c7be484..c6c62fe 100644
--- a/classes/class_db.php
+++ b/classes/class_db.php
@@ -8,7 +8,7 @@
class db
{
// Class wide variables
- var $mysqli;
+ var $dbh;
var $prefix;
var $hits;
@@ -25,20 +25,15 @@ class db
try
{
- $db_port_int = intval($config->db_port);
- $this->mysqli = new mysqli($config->db_host, $config->db_username,
- $config->db_password, $config->db_name, $db_port_int);
+ $dsn = "mysql:host={$config->db_host};dbname={$config->db_name}";
+ if ($config->db_port != '')
+ $dsn .= ";port={$config->db_port}";
- if (!$this->mysqli->connect_error)
- {
- $this->prefix = $config->db_prefix;
- }
- else
- {
- throw new Exception('DB Error');
- }
+ $this->dbh = new PDO($dsn, $config->db_username, $config->db_password);
+ $this->prefix = $config->db_prefix;
+ $this->_affected_rows = 0;
}
- catch (Exception $e)
+ catch (PDOException $e)
{
$title = 'Database error';
$message = 'Database connection failed! Please check your DB settings.';
@@ -56,7 +51,7 @@ class db
$this->hits++;
$recordset = array();
- if (stripos($sql, 'SELECT') !== false && stripos($sql, 'SELECT') == 0)
+ if (stripos($sql, 'SELECT') === 0)
{
// Append limit to single row select query
if ($single)
@@ -65,71 +60,73 @@ class db
}
// Execute the query
- $result = $this->mysqli->query($sql);
+ $statement = $this->dbh->prepare($sql);
+ $result = $statement->execute();
+ $this->_affected_rows = 0;
// Some error occurred
if (!$result)
{
$title = 'Database error';
- $message = "Error: {$this->mysqli->error}<br />";
+ $message = "Error: {$statement->errorInfo()[2]}<br />";
$message .= "Whole query: {$sql}";
$gsod->trigger($title, $message);
}
// Return the data
if (!$single)
- {
- while ($row = $result->fetch_assoc())
- {
- $recordset[] = $row;
- }
+ return $statement->fetchAll(PDO::FETCH_ASSOC);
+ else
+ return $statement->fetch(PDO::FETCH_ASSOC);
+ }
+ else
+ {
+ $statement = $this->dbh->prepare($sql);
+ $result = $statement->execute();
- $result->close();
- return $recordset;
+ // Some error occurred
+ if (!$result)
+ {
+ $title = 'Database error';
+ $message = "Error: {$statement->errorInfo()[2]}<br />";
+ $message .= "Whole query: {$sql}";
+ $gsod->trigger($title, $message);
}
else
{
- $row = $result->fetch_assoc();
- $result->close();
-
- return $row;
+ $this->_affected_rows = $statement->rowCount();
}
}
- else
- {
- $this->mysqli->query($sql);
- }
return true;
}
catch (Exception $e)
{
- return null;
+ return false;
}
}
// Function to get the last inserted query ID
function get_id()
{
- return $this->mysqli->insert_id;
+ return $this->dbh->lastInsertId();
}
// Function to check affected rows
function affected_rows()
{
- return $this->mysqli->affected_rows;
+ return $this->_affected_rows;
}
// Function to escape a special chars string
function escape(&$data)
{
- $data = $this->mysqli->real_escape_string($data);
+ $data = substr($this->dbh->quote($data), 1, -1);
}
- // Object descturtor
+ // Object destructor
function __destruct()
{
- $this->mysqli->close();
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]