1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
class bbno_pdo
{
private $user;
private $pass;
private $db;
private $host;
private $db_sys;
private $to;
private $connection;
private $req;
public function __construct($bbn=1)
{
if ( $bbn = 1 )
{
$this->user = 'user';
$this->pass = 'pass';
$this->db = 'db';
$this->host = 'localhost';
$this->db_sys = 'mysql';
}
else
{
$this->user = $_SESSION['bbn_site']['bbn_db_login'];
$this->pass = $_SESSION['bbn_site']['bbn_db_pass'];
$this->db = $_SESSION['bbn_site']['bbn_db'];
$this->host = $_SESSION['bbn_site']['bbn_db_host'];
$this->db_sys = $_SESSION['bbn_site']['bbn_db_sys'];
}
$this->to = "me@me.com";
}
public function bbnf_db_connect()
{
try
{ $this->connection = new PDO($this->db_sys.':host='.$this->host.';dbname='.$this->db, $this->user, $this->pass ); }
catch (PDOException $e)
{ $this->bbnf_db_error($e); }
}
public function bbnf_db_error($err) // This function sends an email in case of error
{
$msg = $err->getMessage();
if ( method_exists($err,'errorInfo') )
{
$info = $err->errorInfo();
$msg .= '<br /><br />Unified error code: '.$info[0];
$msg .= '<br /><br />Driver specific error code: '.$info[1];
$msg .= '<br /><br />Driver specific error string: '.$info[2];
}
if ( $_SERVER["SERVER_PORT"] != '1975' )
{
$headers .= "From: BaBna <babna@babna.com>\nBcc: babna@babna.com\nMIME-Version: 1.0\ncontent-type: text/html; charset=utf-8\n";
$message = "<html><head><title>PDO Error</title></head><body style=\"color:#FFFFFF; font-family:Verdana, Arial, Helvetica, sans-serif;font-size:13px; background-color:#4f5967\">".$msg."</body></html>\r\n";
mail($this->to,'PDO Error',$message,$headers);
}
else
{ echo $msg; }
echo $msg;
die();
}
public function bbnf_db_query($sql) // This function prepares a query with PDO::Prepare and PDO::bindParam
{
$args = func_get_args();
$i = 1;
$this->req = $this->connection->prepare($sql);
$value = array();
$type = array();
while ( $i < count($args) )
{
if ( is_array($args[$i]) )
{
$value = $args[$i][0];
$type = $args[$i][1];
}
else
{
$value = $args[$i];
$type = 's';
}
if ( $type == 'i' )
{ $type = PDO::PARAM_INT; }
else
{ $type = PDO::PARAM_STR; }
$this->req->bindParam($i,$value,$type);
$i++;
}
return $this->req;
}
public function bbnf_db_recset() //This function executes a query and returns its result
{
try
{ $this->req->execute(); }
catch (PDOException $e)
{ $this->req->bbnf_db_error($e); }
}
public function bbnf_db_exec() //This function executes a query or a transaction and returns the affected rows
{
$args = func_get_args();
// If there are more than one arguments, it is a transaction
if ( count($args) > 1 )
{
$transaction_ok = 1;
$affected_rows = 0;
$this->connection->beginTransaction();
for ( $i = 0; $i < count($args); $i++ )
{
try
{ $affected_rows += $args[$i]->exec(); }
catch (PDOException $e)
{
$transaction_ok = 0;
bbnf_db_error($e);
}
}
if ( $transaction_ok == 1 )
{ $this->connection->commit(); }
else
{ $this->connection->rollBack(); }
}
else
{
try
{ $affected_rows = $args[0]->exec(); }
catch (PDOException $e)
{ $this->bbnf_db_error($e); }
}
return $affected_rows;
}
} |
Partager