Last active
August 14, 2018 08:17
-
-
Save sjccodesnippets/5549589 to your computer and use it in GitHub Desktop.
PHP : MySQLi ultimate connect, query, fetch, and parse result, object oriented oo and procedural
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
<?php | |
mysqli_report(MYSQLI_REPORT_OFF); //Turn off irritating default messages | |
$mysqli = new mysqli("127.0.0.1", "user", "password", "database", 3306); | |
/* check connection */ | |
if (mysqli_connect_errno()) { | |
printf("Connect failed: %s\n", mysqli_connect_error()); | |
exit(); | |
} | |
/* change character set to utf8 */ | |
if (!$mysqli->set_charset("utf8")) { | |
printf("Error loading character set utf8: %s\n", $mysqli->error); | |
} | |
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5"; | |
if ($result = $mysqli->query($query)) { | |
/* fetch object array */ | |
while ($obj = $result->fetch_object()) { | |
printf ("%s (%s)\n", $obj->Name, $obj->CountryCode); | |
} | |
/* | |
// fetch array | |
while ($row = $result->fetch_array()) { | |
$name = $row['Name']; | |
$countrycode = $row['CountrCode']; | |
} | |
// fetch associative array | |
while ($row = $result->fetch_assoc()) { | |
printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]); | |
} | |
// numeric array | |
$row = $result->fetch_array(MYSQLI_NUM); | |
printf ("%s (%s)\n", $row[0], $row[1]); | |
// associative array | |
$row = $result->fetch_array(MYSQLI_ASSOC); | |
printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]); | |
// associative and numeric array | |
printf ("%s (%s)\n", $row[0], $row["CountryCode"]); | |
$row = $result->fetch_array(MYSQLI_BOTH); | |
// LAZY! associative and numeric array | |
$row = $result->fetch_array(); | |
printf ("%s (%s)\n", $row[0], $row["CountryCode"]); | |
// Now if you need to loop through it again, you would first call the seek function: | |
// procedural | |
mysqli_data_seek($result,0); | |
// OO | |
$result->data_seek(399); | |
while ($row = mysqli_fetch_assoc($result)) | |
{ | |
// Looping through the resultset again. | |
} | |
*/ | |
/* free result set */ | |
$result->close(); | |
} | |
// thanks information at saunderswebsolutions dot com | |
if ($mysqli->error) { | |
try { | |
throw new Exception("MySQL error $mysqli->error <br> Query:<br> $query", $msqli->errno); | |
} catch(Exception $e ) { | |
echo "Error No: ".$e->getCode(). " - ". $e->getMessage() . "<br >"; | |
echo nl2br($e->getTraceAsString()); | |
} | |
} | |
// $return['mysqli_affected'] = $mysqli->affected_rows; | |
// $return['mysqli_count'] = $result->num_rows; | |
// $return['mysqli_info'] = $mysqli->info; | |
// $row = $result->fetch_array(); | |
// $row = $result->fetch_assoc(); | |
/* close connection */ | |
$mysqli->close(); | |
/* insert stuff */ | |
$last_id = $result->insert_id; | |
?> | |
<?php | |
/* | |
data for the last week: | |
where | |
date >= curdate() - INTERVAL DAYOFWEEK(curdate())+1 DAY | |
AND | |
date < curdate() - INTERVAL DAYOFWEEK(curdate())-6 DAY | |
*/ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Typo in
msqli
?