Show Menu

MySQLi Cheatsheet Cheat Sheet (DRAFT) by

PHP code snippets on how to use MySQLi

This is a draft cheat sheet. It is a work in progress and is not finished yet.


$db = new mysqli­('l­oca­lhost', 'usern­ame', 'passw­ord', 'datab­ase');

if($db­->c­onn­ect­_errno > 0){
die('Error connecting to database [' . $db->c­onn­ect­_error . ']');
Connecting to a MySQL database using PHP.

MySQLi Properties

number of rows returned
number of rows affected
free up memory from result
close connection to db
$res is the onject storing the query results


$sql = "­SELECT name, phone FROM contacts where contac­tID­=12­";
$results = $db->q­uer­y($­sql);
$rows = $resul­t->­fet­ch_­ass­oc();

// Display results stored in $rows[]
echo "­<p>­$ro­ws[­'na­me'­]</­p>";
echo "­<p>­$ro­ws[­'ph­one­']<­/p>­";