Location: PHPKode > scripts > Simple MySQL DB Class > simple-mysql-db-class/example.php
<?php

/***************************************************************************
*
*	Author   : Eric Sizemore ( www.secondversion.com & www.phpsociety.com )
*	Package  : Simple MySQL DB Class
*	Version  : 1.0.1
*	Copyright: (C) 2007 Eric Sizemore
*	Site     : www.secondversion.com & www.phpsociety.com
*	Email    : hide@address.com
*	File     : example.php
*
*	This program is free software; you can redistribute it and/or modify
*	it under the terms of the GNU General Public License as published by
*	the Free Software Foundation; either version 2 of the License, or
*	(at your option) any later version.
*
*	This program is distributed in the hope that it will be useful,
*	but WITHOUT ANY WARRANTY; without even the implied warranty of
*	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
*	GNU General Public License for more details.
*
***************************************************************************/


/**
* Instantiate our mysql class and connect to the database.
*
* database host/server
* database username
* database password
* database name
*/
$db =& new db_mysql('localhost', 'username', 'password', 'database_name');

/**
* Example 1 - Simple query..
*/
$user = $db->query("
	SELECT *
	FROM users
	WHERE userid = 1
") or $db->raise_error(); // Leaving 'raise_error()' blank will create an error message with the SQL
$user = $db->fetch_array($user);

// OR setting the second param to true, which will return the result set, effectively the same as above
$user = $db->query("
	SELECT *
	FROM users
	WHERE userid = 1
", true) or $db->raise_error(); // Leaving 'raise_error()' blank will create an error message with the SQL

/**
* Example 2 - Getting the number of rows
*/
$users = $db->query("
	SELECT *
	FROM users
") or $db->raise_error(); // Leaving 'raise_error()' blank will create an error message with the SQL
echo $db->num_rows($users);

/**
* Example 3 - Getting the number of affected rows
*/
$users = $db->query("
	UPDATE users
	SET is_active = 0
") or $db->raise_error(); // Leaving 'raise_error()' blank will create an error message with the SQL
echo $db->affected_rows($users);

/**
* Example 4 - Getting the number of executed queries
*/
echo $db->num_queries();

/**
* Example 5 - (un)locking a table/tables
*
* array of  tablename => locktype
*/
// Single table
$tables = array(
	'users' => 'write'
);
$db->lock($tables);

// Multiple tables
$tables = array(
	'users'  => 'write',
	'config' => 'read',
	'posts'  => 'write'
);
$db->lock($tables);

// Unlock
$db->unlock();

/**
* Example 6 - Getting the last insert id of an auto_increment field
*/
$db->query("
	INSERT INTO users (name, email, is_active)
	VALUES ('Eric', 'hide@address.com', 0)
") or $db->raise_error('Failed adding new user'); // Will use the message we give it + the SQL
echo $db->insert_id();

/**
* Example 7 - Freeing a mysql result
*/
$users = $db->query("
	SELECT *
	FROM users
") or $db->raise_error(); // Leaving 'raise_error()' blank will create an error message with the SQL
$db->free_result($users);

/**
* Example 8 - Turning error reporting on
*/
$db->show_errors();

/**
* Example 9 - Turning error reporting off
*/
$db->hide_errors();

/**
* Example 10 - Preparing a value for database queries
*
* Will use mysql_real_escape_string or mysql_escape_string
* depending on your PHP version.
*/
$name = $db->prepare(trim(strip_tags($_POST['name'])));
$email = $db->prepare(trim(strip_tags($_POST['email'])));

$db->query("
	INSERT INTO users (name, email, is_active)
	VALUES ('$name', '$email', 0)
") or $db->raise_error('Failed adding new user'); // Will use the message we give it + the SQL

/**
* Example 11 - Preparing a value for database queries + escaping for LIKE queries
*
* Will use mysql_real_escape_string or mysql_escape_string
* depending on your PHP version.
*/
$email = $db->prepare(trim(strip_tags($_POST['email'])), true);

$db->query("
	SELECT *
	FROM users
	WHERE email LIKE '%$email%'
") or $db->raise_error(); // Leaving 'raise_error()' blank will create an error message with the SQL

/**
* Closing the database connection
*/
$db->close();

?>
Return current item: Simple MySQL DB Class