NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming > CODE
Reload this Page Simple MySQL DB Class

CODE This forum is for posting code snippets and example scripts that aren't quite tutorials, but could be useful for others. You may post code snippets and/or completed scripts that you've written and want to share here.

Advanced Search
9 members in live chat ~  


Closed Thread
 
LinkBack Thread Tools
Old 04-08-2007, 04:28 AM THREAD STARTER               #1 (permalink)
Tech Support
Join Date: Mar 2005
Posts: 4,944
Eric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatness
 

Member of the Month
MOTM September 2005
Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Animal Rescue Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Baby Health Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse Diabetes Protect Our Planet Multiple Sclerosis Autism Adoption Special Olympics

Simple MySQL DB Class


This class is a MySQL database access wrapper. It can:

- Connect to a MySQL server
- Execute SQL queries and retrieve single value results or return result rows into arrays
- Return the last inserted table row identifier, the number of result rows and the number of affected rows
- Count the number of executed queries
- Lock and unlock tables
- Escape text literal values or like patterns
- Retrieve database access errors

mysql.class.php
PHP Code:
<?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    : esizemore05@gmail.com
*    File     : mysql.class.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.
*
***************************************************************************/

/**
* Class to interact with a mysql database
*/
class db_mysql
{
    
/**
    * Connection to MySQL.
    *
    * @var string
    */
    
var $link;

    
/**
    * Holds the most recent connection.
    *
    * @var string
    */
    
var $recent_link null;

    
/**
    * Holds the contents of the most recent SQL query.
    *
    * @var string
    */
    
var $sql '';

    
/**
    * Holds the number of queries executed.
    *
    * @var integer
    */
    
var $query_count 0;

    
/**
    * The text of the most recent database error message.
    *
    * @var string
    */
    
var $error '';

    
/**
    * The error number of the most recent database error message.
    *
    * @var integer
    */
    
var $errno '';

    
/**
    * Do we currently have a lock in place?
    *
    * @var boolean
    */
    
var $is_locked false;

    
/**
    * Show errors? If set to true, the error message/sql is displayed.
    *
    * @var boolean
    */
    
var $show_errors false;

    
/**
    * Constructor. Initializes a database connection and selects our database.
    *
    * @param  string  Database host
    * @param  string  Database username
    * @param  string  Database password
    * @param  string  Database name
    * @return boolean
    */
    
function db_mysql($db_host$db_user$db_pass$db_name)
    {
        
$this->link = @mysql_connect($db_host$db_user$db_pass);

        if (
$this->link)
        {
            if (@
mysql_select_db($db_name$this->link))
            {
                
$this->recent_link =& $this->link;
                return 
$this->link;
            }
        }
        
// If we couldn't connect or select the db...
        
$this->raise_error("Could not select and/or connect to database: $db_name");
    }

    
/**
    * Executes a sql query. If optional $only_first is set to true, it will
    * return the first row of the result as an array.
    *
    * @param  string  Query to run
    * @param  bool    Return only the first row, as an array?
    * @return mixed
    */
    
function query($sql$only_first false)
    {
        
$this->recent_link =& $this->link;
        
$this->sql =& $sql;
        
$result = @mysql_query($sql$this->link);

        
$this->query_count++;

        if (
$only_first)
        {
            
$return $this->fetch_array($result);
            
$this->free_result($result);
            return 
$return;
        }
        return 
$result;
    }

    
/**
    * Fetches a row from a query result and returns the values from that row as an array.
    *
    * @param  string  The query result we are dealing with.
    * @return array
    */
    
function fetch_array($result)
    {
        return @
mysql_fetch_assoc($result);
    }

    
/**
    * Returns the number of rows in a result set.
    *
    * @param  string  The query result we are dealing with.
    * @return integer
    */
    
function num_rows($result)
    {
        return @
mysql_num_rows($result);
    }

    
/**
    * Retuns the number of rows affected by the most recent query
    *
    * @return integer
    */
    
function affected_rows()
    {
        return @
mysql_affected_rows($this->recent_link);
    }

    
/**
    * Returns the number of queries executed.
    *
    * @param  none
    * @return integer
    */
    
function num_queries()
    {
        return 
$this->query_count;
    }

    
/**
    * Lock database tables
????: NamePros.com http://www.namepros.com/code/314241-simple-mysql-db-class.html
    *
    * @param   array  Array of table => lock type
    * @return  void
    */
    
function lock($tables)
    {
        if (
is_array($tables) AND count($tables))
        {
            
$sql '';

            foreach (
$tables AS $name => $type)
            {
                
$sql .= (!empty($sql) ? ', ' '') . "$name $type";
            }

            
$this->query("LOCK TABLES $sql");
            
$this->is_locked true;
        }
    }

    
/**
    * Unlock tables
    */
    
function unlock()
    {
        if (
$this->is_locked)
        {
            
$this->query("UNLOCK TABLES");
            
$this->is_locked false;
        }
    }

    
/**
    * Returns the ID of the most recently inserted item in an auto_increment field
    *
    * @return  integer
    */
    
function insert_id()
    {
        return @
mysql_insert_id($this->link);
    }

    
/**
    * Escapes a value to make it safe for using in queries.
    *
    * @param  string  Value to be escaped
    * @param  bool    Do we need to escape this string for a LIKE statement?
    * @return string
    */
    
function prepare($value$do_like false)
    {
        
$value stripslashes($value);

        if (
$do_like)
        {
            
$value str_replace(array('%''_'), array('\%''\_'), $value);
        }

        if (
function_exists('mysql_real_escape_string'))
        {
            return 
mysql_real_escape_string($value$this->link);
????: NamePros.com http://www.namepros.com/showthread.php?t=314241
        }
        else
        {
            return 
mysql_escape_string($value);
        }
    }

    
/**
    * Frees memory associated with a query result.
    *
    * @param  string   The query result we are dealing with.
    * @return boolean
    */
    
function free_result($result)
    {
        return @
mysql_free_result($result);
    }

    
/**
    * Turns database error reporting on
    */
    
function show_errors()
    {
        
$this->show_errors true;
    }

    
/**
    * Turns database error reporting off
    */
    
function hide_errors()
    {
        
$this->show_errors false;
    }

    
/**
    * Closes our connection to MySQL.
    *
    * @param  none
    * @return boolean
    */
    
function close()
    {
        
$this->sql '';
        return @
mysql_close($this->link);
    }

    
/**
    * Returns the MySQL error message.
    *
    * @param  none
    * @return string
    */
    
function error()
    {
        
$this->error = (is_null($this->recent_link)) ? '' mysql_error($this->recent_link);
        return 
$this->error;
    }

    
/**
    * Returns the MySQL error number.
    *
    * @param  none
    * @return string
    */
    
function errno()
    {
        
$this->errno = (is_null($this->recent_link)) ? mysql_errno($this->recent_link);
        return 
$this->errno;
    }

    
/**
    * Gets the url/path of where we are when a MySQL error occurs.
    *
    * @access private
    * @param  none
    * @return string
    */
    
function _get_error_path()
    {
        if (
$_SERVER['REQUEST_URI'])
        {
            
$errorpath $_SERVER['REQUEST_URI'];
        }
        else
        {
            if (
$_SERVER['PATH_INFO'])
            {
                
$errorpath $_SERVER['PATH_INFO'];
            }
            else
            {
                
$errorpath $_SERVER['PHP_SELF'];
            }

            if (
$_SERVER['QUERY_STRING'])
            {
                
$errorpath .= '?' $_SERVER['QUERY_STRING'];
            }
        }

        if ((
$pos strpos($errorpath'?')) !== false)
        {
            
$errorpath urldecode(substr($errorpath0$pos)) . substr($errorpath$pos);
        }
        else
        {
            
$errorpath urldecode($errorpath);
        }
        return 
$_SERVER['HTTP_HOST'] . $errorpath;
    }

    
/**
    * If there is a database error, the script will be stopped and an error message displayed.
    *
    * @param  string  The error message. If empty, one will be built with $this->sql.
    * @return string
    */
    
function raise_error($error_message '')
    {
        if (
$this->recent_link)
        {
            
$this->error $this->error($this->recent_link);
            
$this->errno $this->errno($this->recent_link);
        }

        if (
$error_message == '')
        {
            
$this->sql "Error in SQL query:\n\n" rtrim($this->sql) . ';';
            
$error_message =& $this->sql;
        }
        else
        {
            
$error_message $error_message . ($this->sql != '' "\n\nSQL:" rtrim($this->sql) . ';' '');
        }

        
$message "<textarea rows=\"10\" cols=\"80\">MySQL Error:\n\n\n$error_message\n\nError: {$this->error}\nError #: {$this->errno}\nFilename: " $this->_get_error_path() . "\n</textarea>";

        if (!
$this->show_errors)
        {
            
$message "<!--\n\n$message\n\n-->";
        }
        die(
"There seems to have been a slight problem with our database, please try again later.<br /><br />\n$message");
    }
}

?>
Example uses
PHP Code:
<?php

/**
* 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', '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();

?>
You can download here or here.


Have fun.
Last edited by SecondVersion; 08-21-2007 at 07:44 PM.
Eric is offline  
Old 04-30-2007, 06:52 AM   #2 (permalink)
Senior Member
 
Hitch's Avatar
Join Date: Aug 2005
Location: Uk, South Yorkshire
Posts: 1,228
Hitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to behold
 


Winner
PHP Programming - May 2007
Animal Rescue Third World Education Find Marrow Donors!
Bump.

Nice Class SV.
Though, what about Ports?

Sure, the user could put 'hostort', but for a "new" coder, they might not know about the :

So, maybe it's better to add another Argument for the port.
Hitch is offline  
Old 05-01-2007, 06:36 PM THREAD STARTER               #3 (permalink)
Tech Support
Join Date: Mar 2005
Posts: 4,944
Eric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatness
 

Member of the Month
MOTM September 2005
Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Animal Rescue Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Baby Health Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse Diabetes Protect Our Planet Multiple Sclerosis Autism Adoption Special Olympics
I'll add in support for ports when I get the time.
Eric is offline  
Old 06-23-2007, 09:57 PM   #4 (permalink)
AzN
is on hiatus
Join Date: May 2006
Posts: 2,449
AzN has a reputation beyond reputeAzN has a reputation beyond reputeAzN has a reputation beyond reputeAzN has a reputation beyond reputeAzN has a reputation beyond reputeAzN has a reputation beyond reputeAzN has a reputation beyond reputeAzN has a reputation beyond reputeAzN has a reputation beyond reputeAzN has a reputation beyond reputeAzN has a reputation beyond repute
 


Find Marrow Donors! Ethan Allen Fund Ethan Allen Fund Ethan Allen Fund Save a Life Save a Life Save a Life Save a Life Save a Life Save a Life VA Tech Memorial VA Tech Memorial VA Tech Memorial VA Tech Memorial Save a Life Save a Life Save a Life
Thanks, I love this class
__________________
Currently on hiatus. Back whenever.
AzN is offline  
Old 07-15-2007, 11:26 PM THREAD STARTER               #5 (permalink)
Tech Support
Join Date: Mar 2005
Posts: 4,944
Eric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatness
 

Member of the Month
MOTM September 2005
Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Animal Rescue Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Baby Health Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse Diabetes Protect Our Planet Multiple Sclerosis Autism Adoption Special Olympics
*bump* no problem AzN

And Hitch... most use the default port - but I may add support for that later.
Eric is offline  
Old 08-17-2007, 05:53 PM   #6 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,074
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
Just been browsing the code section looking at some old posts and I noticed a slight over sight in your class.

In the lock tables function after you lock the tables you set $this->is_locked to true but when you unlock them you never return that variable to false. Therefore you can continuously call the unlock function and it will work execute every time after you locked tables once.
__________________
Manage your portfolio using my new Domain Portfolio Management script.
Securing Your Domain Name From Theft
Peter is offline  
Old 08-21-2007, 07:23 PM   #7 (permalink)
NamePros Member
Join Date: May 2007
Posts: 56
Zurvan is an unknown quantity at this point
 



Here's a possibly silly question:

What are the "@" symbols at the beginning of some lines.

????: NamePros.com http://www.namepros.com/showthread.php?t=314241
ie:

PHP Code:
    function query($sql$only_first false)
    {
        
$this->recent_link =& $this->link;
        
$this->sql =& $sql;
        
$result = @mysql_query($sql$this->link); 
I've never seen that before.
Zurvan is offline  
Old 08-21-2007, 07:46 PM THREAD STARTER               #8 (permalink)
Tech Support
Join Date: Mar 2005
Posts: 4,944
Eric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatness
 

Member of the Month
MOTM September 2005
Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Animal Rescue Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Baby Health Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse Diabetes Protect Our Planet Multiple Sclerosis Autism Adoption Special Olympics
Originally Posted by peter@flexiwebhost
Just been browsing the code section looking at some old posts and I noticed a slight over sight in your class.

In the lock tables function after you lock the tables you set $this->is_locked to true but when you unlock them you never return that variable to false. Therefore you can continuously call the unlock function and it will work execute every time after you locked tables once.
Fixed.

Originally Posted by Zurvan
Here's a possibly silly question:

What are the "@" symbols at the beginning of some lines.

ie:

PHP Code:
    function query($sql$only_first false)
????: NamePros.com http://www.namepros.com/showthread.php?t=314241
    {
        
$this->recent_link =& $this->link;
        
$this->sql =& $sql;
        
$result = @mysql_query($sql$this->link); 
I've never seen that before.
Suppresses errors. http://www.php.net/manual/en/languag...rorcontrol.php
Eric is offline  
Old 09-02-2007, 06:40 AM   #9 (permalink)
Account Suspended
 
Alex.'s Avatar
Join Date: Nov 2006
Location: Uk
Posts: 601
Alex. is on a distinguished road
 


Ethan Allen Fund Third World Education
Thanks Sv! using this on a new site I'm making
Alex. is offline  
Old 09-07-2007, 05:28 PM   #10 (permalink)
Senior Member
 
Porte's Avatar
Join Date: May 2005
Location: I'm right here
Posts: 3,525
Porte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud of
 



Thanks V2, very helpful as usual!
__________________
WP Theme Developer
Your One-stop for Premium Magazine/CMS WordPress Themes
Deluxe Themes
Porte is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


 
All times are GMT -7. The time now is 03:43 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger