PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Eric Sizemore   Simple MySQL DB Class   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Various usage examples
Class: Simple MySQL DB Class
MySQL database access wrapper
Author: By
Last change: Updated to reflect changes in class.
Date: 13 years ago
Size: 5,169 bytes
 

Contents

Class file image Download
<?php

/**
* @author Eric Sizemore <admin@secondversion.com>
* @package Simple MySQL DB Class
* @link www.secondversion.com
* @version 2.0.0
* @copyright (C) 2007 - 2011 Eric Sizemore
* @license GNU Public License
*
* 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 3 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


/**
* Instantiate our mysql class and connect to the database.
*
* database host/server
* database username
* database password
* database name
*
* Class requires $dbconfig array.
*/
$dbconfig = array(
   
'host' => 'localhost',
   
'user' => 'username',
   
'pass' => 'password',
   
'name' => 'database_name'
);

$db = db_mysql::getInstance();

// ################################################################
/**
* 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', 'esizemore05@gmail.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();

?>