Location: PHPKode > scripts > EasyMySQL > easymysql/examples.php
<?php

// Turn off PHP errors and warnings (optional).
// Important for correct retrieving of database access errors.
error_reporting(0);


// Include class file
require_once 'class.mysql.php';


// Create an object
$db = new SmartMySQL();


// connect($server, $username, $password, $database, $persistent = null);
// Note that if you will set $persistent to true closing database won't be required.
$db->connect('localhost', 'root', 'password', 'sampledatabase', true);


// Cache results (Speed it up!)
// Note that chaching works on SELECT like queries excluding ecexute(); method

// Enable cache
$db->cache = true;

// Indicate writable directory for storing files
$db->cachedir = 'cache/';

// Set expiration time in seconds
$db->expire = 300; // 5 Minutes
// Or in a smart way
$db->expire = '5m'; // 5 Minutes again
//Note that you can use m for minutes, h - hours, d - days! 


// execute($query);
$sql = "UPDATE `users` SET `name` = 'John' WHERE `id` = 2;";
$db->execute($sql);


// retrieve($column, $table, $field, $value);
// Query: SELECT `$column` FROM `$table` WHERE `$field` = $value;
// Note that if $value is a number or numeric string, method will automatically remove quotes in where cause.
echo $db->retrieve('last_name','list1','id',1);
echo '<br /><br />';


// fetch($query);
$sql = "SELECT * FROM `users` WHERE `id` = 3;";
$res = $db->fetch($sql);
echo $res['name'] . ' - ' . $res['age'];
echo '<br /><br />';


// fetchAll($query);
$sql = "SELECT * FROM `users`;";
$res = $db->fetchAll($sql);

foreach ($res as $r) {
	$str = $r['name'] . ' - ' . $r['age'];
	echo $str . '<br />';
}

echo '<br /><br />'; // or

$num = count($res);
for ($i=0; $i<$num; $i++) {
	$str = $res[$i]['name'] . ' - ' . $res[$i]['age'];
	echo $str . '<br />';
}


// fetchMode($type);
// Default fetch mode is set to 'assoc'
$db->fetchMode('num');
$sql = "SELECT * FROM `users`;";
$res = $db->fetch($sql);

// or

$db->fetchMode('both');
$sql = "SELECT * FROM `users`;";
$res = $db->fetchAll($sql);


// queryInsert($table, $data = array())
// Note: method automatically validates injected strings with escape() method
$data = array(
	'first_name' => 'John',
	'last_name' => 'Doe',
	'age' => 21
);
$sql = $db->queryInsert('contacts', $data);
echo $sql;

// or

$data['first_name'] = 'John';
$data['last_name'] = 'Doe';
$data['age'] = 21;
$sql = $db->queryInsert('contacts', $data);
echo $sql;

// queryUpdate($table, $data = array(), $other = null)
// You can extend query by adding custom MySQL syntax as $other argument 
// Note: method automatically validates injected strings with escape() method
$data = array(
	'first_name' => 'John',
	'last_name' => 'Doe',
	'age' => 21
);
$sql = $db->queryUpdate('contacts', $data, 'WHERE `id` = 4 LIMIT 1');
echo $sql;


// escape($string);
// Important! Escaping strings is a must-do before using them in queries. It may be the cause of SQL injections.
$content = $db->escape($_POST['content']);
$sql = "INSERT INTO `users` (`biography`) VALUES($content) WHERE `id` = 2;";


// viewData($data);
$sql = "SELECT * FROM `users`;";
$res = $db->fetchAll($sql);
$db->viewData($res);


// close();
// Not required if you use persistent database connection.
$db->close();

?>
Return current item: EasyMySQL