]> git.mxchange.org Git - friendica.git/blobdiff - include/dba.php
Issue 3326: We are now completely working with utf8mb4
[friendica.git] / include / dba.php
index 09d705cbdb9bdb285986ffc73eb69c4b5eaf861c..62728acaed0294ba467cb126cd5ed022f1329e95 100644 (file)
@@ -63,18 +63,21 @@ class dba {
                        $this->db = @new mysqli($server,$user,$pass,$db);
                        if (!mysqli_connect_errno()) {
                                $this->connected = true;
-                       }
-                       if (isset($a->config["system"]["db_charset"])) {
-                               $this->db->set_charset($a->config["system"]["db_charset"]);
+
+                               if (isset($a->config["system"]["db_charset"])) {
+                                       $this->db->set_charset($a->config["system"]["db_charset"]);
+                               }
                        }
                } elseif (function_exists('mysql_connect')) {
                        $this->driver = 'mysql';
                        $this->db = mysql_connect($server,$user,$pass);
                        if ($this->db && mysql_select_db($db,$this->db)) {
                                $this->connected = true;
+
+                               if (isset($a->config["system"]["db_charset"])) {
+                                       mysql_set_charset($a->config["system"]["db_charset"], $this->db);
+                               }
                        }
-                       if (isset($a->config["system"]["db_charset"]))
-                               mysql_set_charset($a->config["system"]["db_charset"], $this->db);
                } else {
                        // No suitable SQL driver was found.
                        if (!$install) {
@@ -215,21 +218,7 @@ class dba {
 
                $this->error = '';
 
-               // Check the connection (This can reconnect the connection - if configured)
-               switch ($this->driver) {
-                       case 'pdo':
-                               // Not sure if this really is working like expected
-                               $connected = ($this->db->getAttribute(PDO::ATTR_CONNECTION_STATUS) != "");
-                               break;
-                       case 'mysqli':
-                               $connected = $this->db->ping();
-                               break;
-                       case 'mysql':
-                               $connected = mysql_ping($this->db);
-                               break;
-               }
-
-               $connstr = ($connected ? "Connected" : "Disonnected");
+               $connstr = ($this->connected() ? "Connected" : "Disonnected");
 
                $stamp1 = microtime(true);
 
@@ -239,9 +228,15 @@ class dba {
                        $sql = "/*".$a->callstack()." */ ".$sql;
                }
 
+               $columns = 0;
+
                switch ($this->driver) {
                        case 'pdo':
                                $result = @$this->db->query($sql);
+                               // Is used to separate between queries that returning data - or not
+                               if (!is_bool($result)) {
+                                       $columns = $result->columnCount();
+                               }
                                break;
                        case 'mysqli':
                                $result = @$this->db->query($sql);
@@ -336,7 +331,7 @@ class dba {
                        }
                }
 
-               if (($result === true) || ($result === false)) {
+               if (is_bool($result)) {
                        return $result;
                }
                if ($onlyquery) {
@@ -347,28 +342,32 @@ class dba {
                $r = array();
                switch ($this->driver) {
                        case 'pdo':
-                               if ($result->rowCount()) {
-                                       while($x = $result->fetch(PDO::FETCH_ASSOC))
-                                               $r[] = $x;
-                                       $result->closeCursor();
+                               while ($x = $result->fetch(PDO::FETCH_ASSOC)) {
+                                       $r[] = $x;
                                }
+                               $result->closeCursor();
                                break;
                        case 'mysqli':
-                               if ($result->num_rows) {
-                                       while($x = $result->fetch_array(MYSQLI_ASSOC))
-                                               $r[] = $x;
-                                       $result->free_result();
+                               while ($x = $result->fetch_array(MYSQLI_ASSOC)) {
+                                       $r[] = $x;
                                }
+                               $result->free_result();
                                break;
                        case 'mysql':
-                               if (mysql_num_rows($result)) {
-                                       while($x = mysql_fetch_array($result, MYSQL_ASSOC))
-                                               $r[] = $x;
-                                       mysql_free_result($result);
+                               while ($x = mysql_fetch_array($result, MYSQL_ASSOC)) {
+                                       $r[] = $x;
                                }
+                               mysql_free_result($result);
                                break;
                }
 
+               // PDO doesn't return "true" on successful operations - like mysqli does
+               // Emulate this behaviour by checking if the query returned data and had columns
+               // This should be reliable enough
+               if (($this->driver == 'pdo') AND (count($r) == 0) AND ($columns == 0)) {
+                       return true;
+               }
+
                //$a->save_timestamp($stamp1, "database");
 
                if ($this->debug) {
@@ -383,19 +382,13 @@ class dba {
                if ($this->result) {
                        switch ($this->driver) {
                                case 'pdo':
-                                       if ($this->result->rowCount()) {
-                                               $x = $this->result->fetch(PDO::FETCH_ASSOC);
-                                       }
+                                       $x = $this->result->fetch(PDO::FETCH_ASSOC);
                                        break;
                                case 'mysqli':
-                                       if ($this->result->num_rows) {
-                                               $x = $this->result->fetch_array(MYSQLI_ASSOC);
-                                       }
+                                       $x = $this->result->fetch_array(MYSQLI_ASSOC);
                                        break;
                                case 'mysql':
-                                       if (mysql_num_rows($this->result)) {
-                                               $x = mysql_fetch_array($this->result, MYSQL_ASSOC);
-                                       }
+                                       $x = mysql_fetch_array($this->result, MYSQL_ASSOC);
                                        break;
                        }
                }