]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/ezSQL/ez_sql_pdo_yourls.php
Add mysql, mysqli and PDO support
[Github/YOURLS.git] / includes / ezSQL / ez_sql_pdo_yourls.php
1 <?php
2
3 class ezSQL_pdo_YOURLS extends ezSQL_pdo {
4
5         /**
6         * Constructor - Overwrite original to use MySQL instead of SQLite
7         * 
8         * @since 1.7
9         */
10         function ezSQL_pdo_YOURLS( $dbuser='', $dbpassword='', $dbname='', $dbhost='localhost', $encoding='' ) {
11                 $this->dbuser = $dbuser;
12                 $this->dbpassword = $dbpassword;
13                 $this->dbname = $dbname;
14                 $this->dbhost = $dbhost;
15                 $this->encoding = $encoding;
16                 $dsn = 'mysql:host=' . $dbhost . ';dbname=' . $dbname ;
17                 $this->dsn = $dsn;
18                 
19                 // Turn on track errors 
20                 ini_set('track_errors',1);
21                 
22                 $this->connect( $dsn, $dbuser, $dbpassword );
23                 
24         }
25         
26
27         /**
28         * Connect to MySQL server. Override original function to allow empty passwords
29         * 
30         * @since 1.7
31         */
32         function connect( $dsn='', $user='', $password='', $ssl=array() ) {
33         
34                 global $ezsql_pdo_str; $return_val = false;
35                 
36                 // Must have a server/db and a user
37                 if ( ! $dsn || ! $user )
38                 {
39                         $this->register_error( $ezsql_pdo_str[1].' in '.__FILE__.' on line '.__LINE__ );
40                         $this->show_errors ? trigger_error($ezsql_pdo_str[1],E_USER_WARNING) : null;
41                 }
42                 
43                 // Establish PDO connection
44                 try 
45                 {
46                         if(!empty($ssl))
47                         {
48                                 $this->dbh = new PDO($dsn, $user, $password, $ssl);
49                         }
50                         else
51                         {
52                                 $this->dbh = new PDO($dsn, $user, $password);
53                         }
54                         
55                         $return_val = true;
56                 } 
57                 catch (PDOException $e) 
58                 {
59                         $this->register_error($e->getMessage());
60                         $this->show_errors ? trigger_error($e->getMessage(),E_USER_WARNING) : null;
61                 }
62
63                 return $return_val;                     
64         }
65
66         /**
67          * Return MySQL server version
68          *
69          * @since 1.7
70          */
71         function mysql_version() {
72                 return ( $this->dbh->getAttribute(PDO::ATTR_SERVER_VERSION) );
73         }
74         
75         /**
76          * Perform mySQL query
77          *
78          * Added to the original function: logging of all queries
79          *
80          * @since 1.7
81          */
82         function query( $query ) {
83         
84                 // Keep history of all queries
85                 $this->debug_log[] = $query;
86
87                 // Original function
88                 return parent::query( $query );
89         }
90
91         /**
92         * Disconnect
93         * 
94         * Actually not needed for PDO it seems, the function is there only for consistency with
95         * other classes
96         *
97         * @since 1.7
98         */
99
100         function disconnect() {
101                 // bleh
102         }       
103         
104
105 }