Skip to content

Instantly share code, notes, and snippets.

@isner
Last active January 3, 2016 04:29
Show Gist options
  • Save isner/8408834 to your computer and use it in GitHub Desktop.
Save isner/8408834 to your computer and use it in GitHub Desktop.
Function that takes your mysqli connection object and a query string and returns a mysql result object
// Performs a database query using PHP's MYSQLI class
// Returns a result object if successful
// Else dies and displays the SQL error and the query string that failed
function db_query($mysqli, $sql) {
$result = $mysqli->query($sql);
if (!$result) {
$message = "SQL error: ".$mysqli->errno." ".$mysqli->error.'<br>';
$message.= "Failed query: ".$sql;
die($message);
} else {
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment