$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) {
$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);
$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);
}
}
- if (($result === true) || ($result === false)) {
+ if (is_bool($result)) {
return $result;
}
if ($onlyquery) {
$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) {
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;
}
}