hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Web Hosting Talk Tutorials : Programming Tutorials : MySQL Class
Reply

Forum Jump

MySQL Class

Reply Post New Thread In Programming Tutorials Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 12-25-2006, 01:45 AM
Dismounted Dismounted is offline
Newbie
 
Join Date: Nov 2005
Location: Australia
Posts: 24

MySQL Class


In just about every site I code, I use PHP & MySQL.

Here is the MySQL PHP Class I always use when I code those sites. I have coded it myself. I call mine mysql.php and use require_once() when it is required.

PHP Code:
<?php
/*================================*\
|| MySQL PHP Class By Hanson Wong ||
||       Copyright (c) 2006       ||
\*================================*/

/*=========================*\
||      Usage Example      ||
\*=========================*/

# require_once('./mysql.php');
# $db = new MySQL;
#
# $db->connect(
#        localhost,
#        test_user,
#        test_pass,
#        test_db
# );
#
# $result = $db->query("SELECT * FROM test_table");
# echo $db->num_rows($result);

class MySQL
{
    
// Define Variables
    
var $errno;
    var 
$error;
    var 
$error_msg;
    var 
$link;

    
/*==========================*\
    || Error Handling Functions ||
    \*==========================*/

    // Get Errors
    
function getError()
    {
        if(empty(
$this->error))
        {
            
$this->errno mysql_errno();
            
$this->error mysql_error();
        }
        return 
$this->errno ": " $this->error;
    }
 
    
// Print Error Message
    
function printError($msg)
    {
        
printf("<b>Error:</b> %s"$msg);
        exit;
    }

    
/*==========================*\
    ||      Main Functions      ||
    \*==========================*/

    // PHP Equivalent: mysql_connect
    
function connect($host$user$pass$db)
    {
        
$this->link mysql_connect($host$user$pass);
         if(!
$this->link)
        {
            
$this->errno 0;
            
$this->error "Connection failed to " $host ".";
            
$this->error_msg $this->errno ": " $this->error;
            return 
$this->printError($this->error_msg);
        }
        elseif(!
mysql_select_db($db$this->link))
        {
            
$this->errno mysql_errno();
            
$this->error mysql_error();
            
$this->error_msg $this->printError($this->getError());
            return 
$this->error_msg;
        }
        else 
        {
            return 
$this->link;
        }
    }
    
    
// PHP Equivalent: mysql_close
    
function close()
    {
        
mysql_close($this->link);
    }
    
    
// PHP Equivalent: mysql_query
    
function query($query)
    {
        
$query mysql_query($query$this->link);
        if(!
$query)
        {
            
$this->error_msg $this->printError($this->getError());
            return 
$this->error_msg;
        }
        else
        {
            return 
$query;
        }
    }

    
// PHP Equivalent: mysql_affected_rows
    
function affected_rows()
    {
        
$query mysql_affected_rows($this->link);
        return 
$query;
    }

    
// PHP Equivalent: mysql_escape_string
    
function escape_string($string)
    {
        
$query mysql_escape_string($string);
        return 
$query;
    }

    
// PHP Equivalent: mysql_fetch_array
    
function fetch_array($query$type)
    {
        
$query mysql_fetch_array($query$type);
        return 
$query;
    }

    
// PHP Equivalent: mysql_fetch_field
    
function fetch_field($query$offset)
    {
        
$query mysql_fetch_field($query$offset);
        if(!
$query)
        {
            
$this->errno 0;
            
$this->error "No information available!";
            
$this->error_msg $this->errno ": " $this->error;
            return 
$this->printError($this->error_msg);
        }
        else
        {
            return 
$query;
        }
    }

    
// PHP Equivalent: mysql_fetch_row
    
function fetch_row($query)
    {
        
$query mysql_fetch_row($query);
        return 
$query;
    }

    
// PHP Equivalent: mysql_field_name
    
function field_name($query$offset)
    {
        
$query mysql_field_name($query$offset);
        return 
$query;
    }

    
// PHP Equivalent: mysql_free_result
    
function free_result($query)
    {
        
mysql_free_result($query);
    }

    
// PHP Equivalent: mysql_insert_id
    
function insert_id()
    {
        
$query mysql_insert_id($this->link);
        return 
$query;
    }

    
// PHP Equivalent: mysql_num_fields
    
function num_fields($query)
    {
        
$query mysql_num_fields($query);
        return 
$query;
    }

    
// PHP Equivalent: mysql_num_rows
    
function num_rows($query)
    {
        
$query mysql_num_rows($query);
        return 
$query;
    }

    
// PHP Equivalent: mysql_real_escape_string
    
function real_escape_string($string)
    {
        
$query mysql_real_escape_string($string$this->link);
        return 
$query;
    }
}
?>
I give permission for you to change/use it on your own sites, the only thing I ask from you is that you keep the copyright intact. The only thing you are NOT allowed to do is use it comercially or sell it for your own profit.



Sponsored Links
  #2  
Old 12-26-2006, 10:56 PM
auveeb auveeb is offline
Junior Guru Wannabe
 
Join Date: Dec 2004
Posts: 49
Cool This will be nifty for some upcoming websites of mine

  #3  
Old 12-29-2006, 02:17 PM
horizon horizon is offline
Web Hosting Master
 
Join Date: Mar 2006
Posts: 961
PHP Code:
# $db->connect(
#        localhost,
#        test_user,
#        test_pass,
#        test_db
# ); 
The SQL server configuration could rather be added on one configuration file (like: config.php file). Then, the:

PHP Code:
 $db->connect(localhosttest_usertest_passtest_db); 
could rather be used under the connect method name under the class routine you posted above - by using: this->connect($host, $user, $pass, $db);.

This way, users won't have to call a connection each times they would create an SQL statement.

Sponsored Links
  #4  
Old 01-04-2007, 09:36 AM
nik0las nik0las is offline
New Member
 
Join Date: Jan 2007
Posts: 3
Nice class. The only thing I would suggest is that you take a look at the singleton design pattern. This is a perfect canidate.

  #5  
Old 01-05-2007, 03:29 AM
Marble Marble is offline
Web Hosting Master
 
Join Date: Dec 2003
Location: Vancouver BC, eh?
Posts: 570

  #6  
Old 07-08-2009, 01:02 PM
Senrax Senrax is offline
New Member
 
Join Date: Jun 2009
Location: NY
Posts: 0
Thanks this helped me out

  #7  
Old 07-17-2009, 02:44 PM
thesecret thesecret is offline
Newbie
 
Join Date: May 2009
Posts: 6
thanks alot

  #8  
Old 07-25-2009, 01:07 PM
dungho dungho is offline
Newbie
 
Join Date: Jun 2009
Posts: 16
Thanks in advanced

  #9  
Old 08-28-2009, 11:51 AM
A J C A J C is offline
WHT Addict
 
Join Date: Aug 2009
Location: Wolverhampton, England
Posts: 147
Very useful, thank you.

  #10  
Old 10-12-2009, 05:34 AM
abhishekkaushik abhishekkaushik is offline
New Member
 
Join Date: Mar 2007
Posts: 4
good script man...
i suggest than you keep the dbbase user/pass etc into config file and include this file in constructor of this function where you can directly connect to database

  #11  
Old 11-03-2009, 02:29 PM
HighRevHosting HighRevHosting is offline
Newbie
 
Join Date: Oct 2009
Location: Canada
Posts: 17
Very nice script. I got one pretty much the exact same. Anyone try using this while connected to multiple databases?

  #12  
Old 11-22-2009, 09:47 PM
kerber kerber is offline
Newbie
 
Join Date: Sep 2009
Posts: 6
Quote:
Originally Posted by Marble View Post
Using this object can create a problem on a very active site.
Can you please elaborate why? (cause, its not small thing to play with..)

  #13  
Old 11-23-2009, 12:22 AM
petteyg359 petteyg359 is offline
Web Hosting Master
 
Join Date: Mar 2009
Posts: 609
Quote:
Originally Posted by kerber View Post
Can you please elaborate why? (cause, its not small thing to play with..)
Stated in a previous post.

Quote:
Originally Posted by nik0las View Post
Nice class. The only thing I would suggest is that you take a look at the singleton design pattern. This is a perfect canidate.

__________________
[GB ≠ GiB] [MB ≠ MiB] [kB ≠ kiB] [1000 ≠ 1024] [Giga ≠ gram] [Mega ≠ milli] [Kelvin ≠ kilo] [Byte ≠ bit]
There is no millibit. There is no gram-bit. There is no Kelvin-Byte.

  #14  
Old 11-28-2009, 04:41 AM
tim2718281 tim2718281 is offline
Web Hosting Master
 
Join Date: Mar 2009
Posts: 2,218
Quote:
Originally Posted by kerber View Post
Can you please elaborate why? (cause, its not small thing to play with..)
I think they are assuming that a programmer using the class will issue multiple connects and closes within a web page.

And they are also assuming there is a way to modify the class to only issue a single connect and close to the SQL server, even if the programmer were to invoke connect and close multiple times with a page.

I can't work out how to do that, myself. Maybe there is an explanation somewhere.

I can see you could have a counter, incremented and decremented whenever connect and close is invoked. If a connect is issued when there is already a connection, the existing connection is used and the counter incremented. And the connection is only closed when the counter goes from 1 to 0.

But won't most web pages with multiple connects issue them in the series connect/disconnect/connect/disconect etc?

Maybe I'm simply failing to see the idea.

  #15  
Old 11-28-2009, 06:04 AM
tim2718281 tim2718281 is offline
Web Hosting Master
 
Join Date: Mar 2009
Posts: 2,218
Quote:
Originally Posted by tim2718281 View Post
I think they are assuming that a programmer using the class will issue multiple connects and closes within a web page.

And they are also assuming there is a way to modify the class to only issue a single connect and close to the SQL server, even if the programmer were to invoke connect and close multiple times with a page.
Ah; maybe I've got the wrong idea.

Maybe the idea is not to prevent multiple connects and disconnects within a web page, but to ensure the web page has at most one connection at once.

Reply

Related posts from TheWhir.com
Title Type Date Posted
Google Releases Hosted Database Service Cloud SQL to General Availability Web Hosting News 2014-02-12 13:46:02
HostingDiscussion Listing 2014-07-30 03:27:07
CubicWebs Listing 2014-07-30 03:27:27
Google Cloud Provides Support For Native MySQL Connections Web Hosting News 2013-11-01 14:36:06
PHP And MySQL Scaling: Preparing A Startup For Growth Blog 2014-04-24 13:27:35


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?