Skip to content

Instantly share code, notes, and snippets.

@seantrane
Last active April 21, 2018 13:16
Show Gist options
  • Save seantrane/3794196 to your computer and use it in GitHub Desktop.
Save seantrane/3794196 to your computer and use it in GitHub Desktop.
PHP: MySQLi (generic usage example)
$mysqli = new mysqli($host,$user,$pass,$db,$port);
if ($mysqli->connect_errno)
{
error_log('Database Connection Error: '.$db.' (URI: '.$_SERVER["REQUEST_URI"].'): #'.$mysqli->connect_errno.' - '.$mysqli->connect_error, 0);
return false;
}
else
{
$aReturn = array();
if ($result = $mysqli->query($sql))
{
$aReturn['num_rows'] = (isset($result->num_rows)) ? $result->num_rows : 0;
$aReturn['field_count'] = (isset($result->field_count)) ? $result->field_count : null;
$aReturn['thread_id'] = (isset($result->thread_id)) ? $result->thread_id : null;
if ($aReturn['num_rows'] > 0)
{
$aResults = array();
while ($row = $result->fetch_object()) $aResults[] = $row;
$aReturn['results'] = (object)$aResults;
}
else
{
$aReturn['results'] = null;
}
return (object)$aReturn;
}
else
{
error_log('MySQL Error: #'.$mysqli->errno.' - '.$mysqli->error, 0);
return false;
}
$result->free();
}
$mysqli->close();
unset($mysqli);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment