
12-25-2006, 01:45 AM
|
|
Newbie
|
|
Join Date: Nov 2005
Location: Australia
Posts: 24
|
|
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.
|

12-26-2006, 10:56 PM
|
|
Junior Guru Wannabe
|
|
Join Date: Dec 2004
Posts: 49
|
|
Cool  This will be nifty for some upcoming websites of mine 
|

12-29-2006, 02:17 PM
|
|
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(localhost, test_user, test_pass, test_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. 
|

01-04-2007, 09:36 AM
|
|
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.
|

01-05-2007, 03:29 AM
|
|
Web Hosting Master
|
|
Join Date: Dec 2003
Location: Vancouver BC, eh?
Posts: 570
|
|
|

07-08-2009, 01:02 PM
|
|
New Member
|
|
Join Date: Jun 2009
Location: NY
Posts: 0
|
|
Thanks this helped me out
|

07-17-2009, 02:44 PM
|
|
Newbie
|
|
Join Date: May 2009
Posts: 6
|
|
|

07-25-2009, 01:07 PM
|
|
Newbie
|
|
Join Date: Jun 2009
Posts: 16
|
|
|

08-28-2009, 11:51 AM
|
|
WHT Addict
|
|
Join Date: Aug 2009
Location: Wolverhampton, England
Posts: 142
|
|
|

10-12-2009, 05:34 AM
|
|
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-03-2009, 02:29 PM
|
|
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?
|

11-22-2009, 09:47 PM
|
|
Newbie
|
|
Join Date: Sep 2009
Posts: 6
|
|
Quote:
Originally Posted by Marble
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..)
|

11-23-2009, 12:22 AM
|
|
Web Hosting Master
|
|
Join Date: Mar 2009
Posts: 596
|
|
Quote:
Originally Posted by kerber
Can you please elaborate why? (cause, its not small thing to play with..)
|
Stated in a previous post.
Quote:
Originally Posted by nik0las
Nice class. The only thing I would suggest is that you take a look at the singleton design pattern. This is a perfect canidate.
|
|

11-28-2009, 04:41 AM
|
|
Web Hosting Master
|
|
Join Date: Mar 2009
Posts: 2,218
|
|
Quote:
Originally Posted by kerber
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.
|

11-28-2009, 06:04 AM
|
|
Web Hosting Master
|
|
Join Date: Mar 2009
Posts: 2,218
|
|
Quote:
Originally Posted by tim2718281
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.
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
| 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
HTML code is Off
|
|
|
|
|
|
| Login: |
|
|
| Advertisement: |
|
|
| Web Hosting News: |
|
|
|