Show Menu
Cheatography

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.

Connect

$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

$res->­num­_rows
number of rows returned
$res->­aff­ect­ed_rows
number of rows affected
$res->­free()
free up memory from result
$db->c­lose()
close connection to db
$res is the onject storing the query results
 

Query

$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>­";