<?php
/**
* Example CrutchMySQL
*/
require('CrutchMySQL.php');
$db = new CrutchMySQL('root','','mysql');
// connect(FALSE): non persistent connect
$db->connect();
/**
* ERROR FOR DEBUG EXAMPLE
*/
echo '<hr><h1>debug()</h1>';
echo 'debug(ERROR)';
$db->debug('ERROR');
$db->select('example');
echo 'debug(EXTEND)';
$db->debug('EXTEND');
$db->select('example');
echo 'debug(DETAIL)';
$db->debug('DETAIL');
$db->select('example');
echo '<hr>';
// example table
$sql = "
CREATE TABLE IF NOT EXISTS `example` (
`id` SMALLINT(5) UNSIGNED AUTO_INCREMENT NOT NULL,
`key` VARCHAR(10) NOT NULL,
`value` VARCHAR(50) NULL,
PRIMARY KEY (`id`),
UNIQUE KEY (`key`)
) ENGINE=InnoDB
";
$db->execute($sql);
//example SQL
$sql = "SELECT `key`,`value` FROM `example`";
/**
* INSERT
*/
echo '<hr><h1>insert()</h1>';
$data = array(
'key' => 'class',
'value' => 'CrutchMySQL',
);
$last_id[] = $db->insert('example',$data);
$data = array(
'key' => 'author',
'value' => 'Lawondyss',
);
$last_id[] = $db->insert('example',$data);
$data = array(
'key' => 'blog',
'value' => 'http://lawondyss.posterous.com'
);
$last_id[] = $db->insert('example',$data);
$data = array(
'key' => 'version',
'value' => '1.0'
);
$last_id[] = $db->insert('example',$data);
$data = array(
'key' => 'url',
'value' => NULL
);
$last_id[] = $db->insert('example',$data);
var_dump($last_id);
/**
* SELECT
*/
echo '<hr><h1>select()</h1>';
while($row = $db->select('example'))
{
var_dump($row);
}
while($row = $db->select('example','ISNULL(value)'))
{
var_dump($row);
}
/**
* REPLACE
*/
echo '<hr><h1>replace()</h1>';
$data = array(
'key' => 'url',
'value' => 'none'
);
$last_id = $db->replace('example',$data);
var_dump($last_id);
while($row = $db->select('example'))
{
var_dump($row);
}
/**
* TRANSACTION, DELETE, FETCH ALL
*/
echo '<hr><h1>transaction, delete(), fetchAll()</h1>';
$db->begin();
echo 'fetchAll(result)';
$result = $db->execute($sql);
while($row = $db->fetchAll($result))
{
var_dump($row);
}
echo 'delete()';
var_dump($db->delete('example','value="none"'));
while($row = $db->select('example'))
{
var_dump($row);
}
$db->rollback();
echo 'fetchAll(SQL)';
while($row = $db->fetchAll($sql))
{
var_dump($row);
}
/**
* FETCH SINGLE, CELL, FIELD
*/
echo '<hr><h1>fetchSingle(resource)</h1>';
$result = $db->execute($sql);
var_dump($db->fetchSingle($result));
echo '<hr><h1>fetchSingle(SQL)</h1>';
var_dump($db->fetchSingle($sql));
echo '<hr><h1>fetchCell(resource, field, row)</h1>';
$result = $db->execute($sql);
var_dump($db->fetchCell($result, 'value'));
echo '<hr><h1>fetchCell(SQL)</h1>';
var_dump($db->fetchCell($sql));
echo '<hr><h1>fetchField(SQL)</h1>';
var_dump($db->fetchField($sql));
/**
* COUNT ROWS, FIELDS, AFFECTED ROWS
*/
echo '<hr><h1>countRows()</h1>';
$result = $db->execute($sql);
var_dump($db->countRows($result));
echo '<hr><h1>countFields()</h1>';
$result = $db->execute($sql);
var_dump($db->countFields($result));
echo '<hr><h1>affectedRows()</h1>';
$db->execute("REPLACE INTO `example` (`key`,`value`) VALUES('url','http://www.example.com')");
var_dump($db->affectedRows());
echo '<hr>';
//drop example table
$db->execute("DROP TABLE IF EXISTS example");
?>
<style type="text/css">
PRE{
margin: 5px;
padding: 5px;}
PRE.error{
border: 1px solid #900;
background-color: #F99;}
PRE.sql{
border: 1px solid #090;
background-color: #9F9;}
</style>
|