<?php
// class to connect to MySQL and perform SQL queries
// From - http://coursesweb.net/php-mysql/
class BaseConn {
static protected $conn = false; // stores the connection to mysql
protected $conn_data = array(); // to store data for connecting to database
public $affected_rows = 0; // number affected rows for Insert, Update, Delete
public $num_rows = 0; // number of rows from Select /Show results
public $last_insertid; // stores the last ID in an AUTO_INCREMENT column, after Insert query
public $currentpage = array('nrac'=>1, 'dt'=>1); // number of accesses, and last accessed of current accessed page
protected $l; // to store the text used in script
public $eror = false; // to store and check for errors
function __construct($conn_data) {
$this->l = $GLOBALS['lang_txt']; // store in property the texts defined in 'lang.php'
if(STORE_DATA == 'mysql') $this->conn_data = $conn_data; // stores connection data to MySQL database
// filter data recived via POST for page access
if(isset($_POST['url']) && isset($_POST['title'])) {
$protocol = (isset($_SERVER["HTTPS"]) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
$_POST['url'] = trim(strip_tags(str_replace($protocol.'://'.$_SERVER['HTTP_HOST'], '', $_POST['url'])));
$_POST['title'] = trim(str_replace(array('<', '>', '"', "'"), array('<', '>', '"', '''), $_POST['title']));
// SAVE_WTD = 1 allows to save data
if(!defined('SAVE_WTD')) define('SAVE_WTD', (ISAJAX == 1 && (!isset($_SESSION['pgaurl']) || (isset($_POST['url']) && $_POST['url'] != $_SESSION['pgaurl']))) ? 1 : 0);
}
}
// for connecting to mysql with PDO
protected function setConn($conn_data) {
try {
// Connect and create the PDO object
self::$conn = new PDO("mysql:host=".$conn_data['host']."; dbname=".$conn_data['bdname'], $conn_data['user'], $conn_data['pass']);
// Sets to handle the errors in the ERRMODE_EXCEPTION mode
self::$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Sets transfer with encoding UTF-8
self::$conn->exec('SET character_set_client="utf8",character_set_connection="utf8",character_set_results="utf8";');
}
catch(PDOException $e) {
$this->eror = $e->getMessage();
}
}
// Performs SQL queries
// $sql - SQL query with prepared statement
// $val - array of insert values
public function sqlExecute($sql, $val=array()) {
if(self::$conn === false || self::$conn === NULL) $this->setConn($this->conn_data); // sets the connection to mysql
$re = true; // the value to be returned
// if there is a connection set ($conn property not false)
if(self::$conn !== false) {
// gets the first word in $sql, to determine whenb SELECT query
$ar_mode = explode(' ', trim($sql), 2);
$mode = strtolower($ar_mode[0]);
// performs the query and get returned data
try {
$sqlre = self::$conn->prepare($sql); // prepares statement
// execute query
if($sqlre && $sqlre->execute($val)) {
// if $mode is 'select' or 'show', gets the result_set to return
if($mode == 'select' || $mode == 'show') {
$re = array();
// if fetch() returns at least one row (not false), adds the rows in $re for return
if(($row = $sqlre->fetch(PDO::FETCH_ASSOC)) !== false){
do {
// check each column if it has numeric value, to convert it from "string"
foreach($row AS $k=>$v) {
if(is_numeric($v)) $row[$k] = $v + 0;
}
$re[] = $row;
}
while($row = $sqlre->fetch(PDO::FETCH_ASSOC));
}
$this->num_rows = count($re); // number of returned rows
}
else $this->affected_rows = $sqlre->rowCount(); // affected rows for Insert, Update, Delete
// if Insert query, stores the last insert ID
if($mode == 'insert') $this->last_insertid = self::$conn->lastInsertId();
}
else $this->setSqlError(self::$conn->errorInfo());
}
catch(PDOException $e) {
$this->setSqlError(array(0, 1, $e->getMessage()));
}
}
// sets to return false in case of error
if($this->eror !== false) $re = false;
return $re;
}
// set sql error 8n $eror
// $errorInfo - array with error (error message stored in index 2)
protected function setSqlError($errorInfo) {
$this->eror = '<h4>'. $this->l['err']. $errorInfo[2] .'</h4>';
}
}
|