<?php

// If using MySQL class
require 'class.mysql.php';
$db = new mysql_cls('localhost''db_user''db_pass''db_db');

// Or, if using MySQLI class
require 'class.mysqli.php';
$db = new mysqli_cls('localhost''db_user''db_pass''db_db');

// Querying
$db->query('SELECT `field` FROM `table`');

/*
* Pre-formatted queries [CRUD]
*/

// Create [insert]
// Param 1: table
// Param 2: associative array in field => value format
// Param 3: sanitisation flag (boolean)
$db->insert('mytable', array('field1' => 'Value!''field2' => 'Another value'), true);

// Retrieve [select]
// Param 1: table
// Param 2: array of fields to select
// Param 3: query parameters; generally an SQL WHERE clause
$db->select('mytable', array('name''age'), 'WHERE `name` = \'Alan\'');

// Update [update]
// Param 1: table
// Param 2: associative array in field => value format
// Param 3: parameters (usually SQL WHERE clause)
// Param 4: sanitisation flag (boolean)
$db->update('mytable', array('field1' => 'Updated value!'), 'WHERE `id` = \'5\''false);

// Delete [delete]
// Param 1: table
// Param 2: query parameters
$db->delete('mytable''WHERE `age` < \'16\'');

// Fetch data
$db->select('mytable', array('name''age'), 'ORDER BY `id` DESC LIMIT 4');
while(
$row $db->fetch())
    echo 
'<p>'$row['name'], ' - '$row['age'], ' years old.</p>';

// Rows returned
$db->select('mytable', array('name''age'), 'ORDER BY `id` DESC LIMIT 4');
if(
$db->numrows() > 0)
{
    echo 
$db->numrows(), 'users found!';
    while(
$row $db->fetch())
        echo 
'<p>'$row['name'], ' - '$row['age'], ' years old.</p>';
}
else
    echo 
'No users found. :(';

/*
* End pre-formatted queries [CRUD]
*/

// Sanitisation (XSS/SQL injection)
$userinput '<script>Malicious input</script>';
echo 
$db->sanitise($userinput); // Returns &lt;script&gt;Malicious input&lt;/script&gt;

// Query count
$db->delete('mytable''WHERE `age` < \'16\'');
$db->insert('mytable', array('name' => 'paedophile66''age' => '52'), true);

echo 
$db->querycount(), ' queries!';

?>