Skip to content

Instantly share code, notes, and snippets.

@birkir
Created November 28, 2013 10:05
Show Gist options
  • Save birkir/7689700 to your computer and use it in GitHub Desktop.
Save birkir/7689700 to your computer and use it in GitHub Desktop.
Multiquery adapter for MySQLi database driver
<?php
class MySQLi_Database extends Database {
...
public function multiquery($sql)
{
// Make sure the database is connected
$this->_connection or $this->connect();
if (Kohana::$profiling)
{
// Benchmark this query for the current instance
$benchmark = Profiler::start("Database ({$this->_instance})", $sql);
}
$result = mysqli_multi_query($this->_connection, $sql);
// Execute the query
if ($result === FALSE)
{
if (isset($benchmark))
{
// This benchmark is worthless
Profiler::delete($benchmark);
}
throw new Database_Exception(':error [ :query ]',
array(':error' => mysqli_error($this->_connection), ':query' => $sql),
mysqli_errno($this->_connection));
}
if (isset($benchmark))
{
Profiler::stop($benchmark);
}
// Set the last query
$this->last_query = $sql;
$output = [];
do
{
if ($result = mysqli_store_result($this->_connection))
{
while ($row = mysqli_fetch_row($result))
{
$output[] = $row[0];
}
mysqli_free_result($result);
}
if (mysqli_next_result($this->_connection))
$output[] = 'multiquery done.';
}
while (mysqli_more_results($this->_connection));
// return affected rows
return implode("\n", $output);
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment