Better separation between queries with or without result

This commit is contained in:
Michael 2017-03-07 17:16:17 +00:00
parent d686bdcf09
commit f09a8609df

View file

@ -242,9 +242,13 @@ class dba {
$sql = "/*".$a->callstack()." */ ".$sql; $sql = "/*".$a->callstack()." */ ".$sql;
} }
$columns = 0;
switch ($this->driver) { switch ($this->driver) {
case 'pdo': case 'pdo':
$result = @$this->db->query($sql); $result = @$this->db->query($sql);
// Is used to separate between queries that returning data - or not
$columns = $result->columnCount();
break; break;
case 'mysqli': case 'mysqli':
$result = @$this->db->query($sql); $result = @$this->db->query($sql);
@ -350,31 +354,30 @@ class dba {
$r = array(); $r = array();
switch ($this->driver) { switch ($this->driver) {
case 'pdo': case 'pdo':
if ($result->rowCount()) { while ($x = $result->fetch(PDO::FETCH_ASSOC)) {
while($x = $result->fetch(PDO::FETCH_ASSOC)) $r[] = $x;
$r[] = $x;
$result->closeCursor();
} }
$result->closeCursor();
break; break;
case 'mysqli': case 'mysqli':
if ($result->num_rows) { while ($x = $result->fetch_array(MYSQLI_ASSOC)) {
while($x = $result->fetch_array(MYSQLI_ASSOC)) $r[] = $x;
$r[] = $x;
$result->free_result();
} }
$result->free_result();
break; break;
case 'mysql': case 'mysql':
if (mysql_num_rows($result)) { while ($x = mysql_fetch_array($result, MYSQL_ASSOC)) {
while($x = mysql_fetch_array($result, MYSQL_ASSOC)) $r[] = $x;
$r[] = $x;
mysql_free_result($result);
} }
mysql_free_result($result);
break; break;
} }
if (($this->driver == 'pdo') AND (strtolower(substr($orig_sql, 0, 6)) != "select") AND (count($r) == 0)) { // PDO doesn't return "true" on successful operations - like mysqli does
// mysqli separates the return value between "select" and "update" - pdo doesn't // Emulate this behaviour by checking if the query returned data and had columns
$r = true; // This should be reliable enough
if (($this->driver == 'pdo') AND (count($r) == 0) AND ($columns == 0)) {
return true;
} }
//$a->save_timestamp($stamp1, "database"); //$a->save_timestamp($stamp1, "database");
@ -391,19 +394,13 @@ class dba {
if ($this->result) { if ($this->result) {
switch ($this->driver) { switch ($this->driver) {
case 'pdo': case 'pdo':
if ($this->result->rowCount()) { $x = $this->result->fetch(PDO::FETCH_ASSOC);
$x = $this->result->fetch(PDO::FETCH_ASSOC);
}
break; break;
case 'mysqli': case 'mysqli':
if ($this->result->num_rows) { $x = $this->result->fetch_array(MYSQLI_ASSOC);
$x = $this->result->fetch_array(MYSQLI_ASSOC);
}
break; break;
case 'mysql': 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; break;
} }
} }