/var/www/globavet.com/webadmin/inc/Db.php


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
<?php

require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'Pdotester.php';

/*
 * Database helper for PDO-MySQL
 * init() before use
 *
 *
 */

class Db{

    public static 
$dbh;

    public static function 
init($dsn$user$password){
        try {
            
$options = array(
                    
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
            
);
            
//self::$dbh = new PDO($dsn, $user, $password, $options);
            
self::$dbh = new PDOTester($dsn$user$password$options);

        } catch (
PDOException $e) {
            
//send email report to system admin
            //$headers    = 'From: '.ADMIN_EMAIL."\r\n";
            
$subject    "[OneERP]Could not connect to DB";
            
$msg        'Connection failed: ' $e->getMessage();
            
//mail(ADMIN_EMAIL, $subject, $msg, $headers);
            //log_write($e);
            //myerror("$subject", false);
            //header("Location: ".ERROR_PAGE);
            
print $subject;
            
//print $msg;
            
exit;
        }
        return 
self::getDbh();
    }


    public static function 
getDbh(){
        return 
self::$dbh;
    }

    
//depreciated, please use $sth->error()
    
public static function showerror($sth$q$param=''){
        
//global $sth;
        
if(!$q && DEBUG>)
        {
            print
"Execute query error, because: <br/><pre>";
            print 
$sth->getSQL().HTML_EOL;
            
var_dump$sth->errorInfo() );
            print
"</pre>";

            
$dump error_get_last();
            
//adderrorlog( 'SQL error', $dump['message'].PHP_EOL.$sth->getSQL( (array)$param) );
            
exit;
        }
    }

    
//new method
    
public function error($sth$q$param=array()){
        
//global $sth;
        
if(!$q && DEBUG>)
        {
            print
"Execute query error, because: <br/><pre>";
            print 
$sth->getSQL($param).HTML_EOL;
            
var_dump$sth->errorInfo() );
            print
"</pre>";

            
$dump error_get_last();
            
//adderrorlog( 'SQL error', $dump['message'].PHP_EOL.$this->getSQL( (array)$param) );
            
exit;
        }
    }


    
//return all rows of the query
    
public static function all($sql$sql_param=array()){
        
$sth self::getDbh()->prepare($sql$sql_param);
        
$sth->execute$sql_param );
        
//echo $sth->getSQL( $sql_param ) . HTML_EOL;
        
$rows $sth->fetchAll(PDO::FETCH_ASSOC);
        return 
$rows;
    }

    
//return first row of the query
    
public static function first($sql$sql_param=array()){
        
$tmp self::all($sql$sql_param);
        return 
$tmp[0];
    }

    
//return first row and column of the query
    
public static function val($sql$sql_param=array()){
        
$sth self::getDbh()->prepare($sql);
        
$q $sth->execute$sql_param );
        
//echo $sth->getSQL( $sql_param ) . HTML_EOL;
        
pdo_showerror($sth$q);
        return 
$sth->fetchColumn();
    }

    public static function 
uuid(){
        
$sql "SELECT UUID() ";
        return 
self::val($sql);
    }

    
//warpper functions
    
public static function beginTransaction(){
        
self::$dbh->beginTransaction();
    }
    public static function 
commit(){
        
self::$dbh->commit();
    }
    public static function 
rollBack(){
        
self::$dbh->rollBack();
    }
}