Created
November 8, 2012 22:29
-
-
Save AmyStephen/4042212 to your computer and use it in GitHub Desktop.
Joomla Platform MySQLI DB Driver - overlays error
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Execute the SQL statement. | |
* | |
* @return mixed A database cursor resource on success, boolean false on failure. | |
* | |
* @since 12.1 | |
* @throws RuntimeException | |
*/ | |
public function execute() | |
{ | |
$this->connect(); | |
if (!is_object($this->connection)) | |
{ | |
JLog::add(JText::sprintf('JLIB_DATABASE_QUERY_FAILED', $this->errorNum, $this->errorMsg), JLog::ERROR, 'database'); | |
throw new RuntimeException($this->errorMsg, $this->errorNum); | |
} | |
// Take a local copy so that we don't modify the original query and cause issues later | |
$sql = $this->replacePrefix((string) $this->sql); | |
if ($this->limit > 0 || $this->offset > 0) | |
{ | |
$sql .= ' LIMIT ' . $this->offset . ', ' . $this->limit; | |
} | |
// Increment the query counter. | |
$this->count++; | |
// If debugging is enabled then let's log the query. | |
if ($this->debug) | |
{ | |
// Add the query to the object queue. | |
$this->log[] = $sql; | |
JLog::add($sql, JLog::DEBUG, 'databasequery'); | |
} | |
// Reset the error values. | |
$this->errorNum = 0; | |
$this->errorMsg = ''; | |
// Execute the query. Error suppression is used here to prevent warnings/notices that the connection has been lost. | |
$this->cursor = @mysqli_query($this->connection, $sql); | |
if ((int)mysqli_errno($this->connection) > 0) { | |
echo '<br />After $this->cursor - display the untrapped error before it is overwritten by the next DB operation: '; | |
echo '<br /> Code: ' . (int)mysqli_errno($this->connection). ': '; | |
echo '<br /> Error: ' . (string)mysqli_error($this->connection). ': <br /><br />'; | |
} | |
// If an error occurred handle it. | |
if (!$this->cursor) | |
{ | |
echo '<br />There is no cursor and errors are still available since no more DB operations have taken place. '; | |
echo '<br /> Code: ' . (int)mysqli_errno($this->connection). ': '; | |
echo '<br /> Error: ' . (string)mysqli_error($this->connection). ': <br /><br />'; | |
// Check if the server was disconnected. | |
if (!$this->connected()) | |
{ | |
try | |
{ | |
// Attempt to reconnect. | |
$this->connection = null; | |
$this->connect(); | |
} | |
// If connect fails, ignore that exception and throw the normal exception. | |
catch (RuntimeException $e) | |
{ | |
$this->errorNum = (int) mysqli_errno($this->connection); | |
$this->errorMsg = (string) mysqli_error($this->connection) . ' SQL=' . $sql; | |
JLog::add(JText::sprintf('JLIB_DATABASE_QUERY_FAILED', $this->errorNum, $this->errorMsg), JLog::ERROR, 'databasequery'); | |
throw new RuntimeException($this->errorMsg, $this->errorNum); | |
} | |
// Since we were able to reconnect, run the query again. | |
return $this->execute(); | |
} | |
// The server was not disconnected. | |
else | |
{ | |
$this->errorNum = (int) mysqli_errno($this->connection); | |
$this->errorMsg = (string) mysqli_error($this->connection) . ' SQL=' . $sql; | |
echo '<br />The connection is fine. The error code is zero now gone because the last DB operation -- $this->connection() -- was successful and overlaid the error codes. '; | |
echo '<br /> Code: ' . $this->errorNum . ': '; | |
echo '<br /> Error: ' . $this->errorMsg . ': <br /><br />'; | |
die; | |
JLog::add(JText::sprintf('JLIB_DATABASE_QUERY_FAILED', $this->errorNum, $this->errorMsg), JLog::ERROR, 'databasequery'); | |
throw new RuntimeException($this->errorMsg, $this->errorNum); | |
} | |
} | |
return $this->cursor; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment