Results 1 to 17 of 17
Thread: MySQL Class
-
12-25-2006, 01:45 AM #1Newbie
- Join Date
- Nov 2005
- Location
- Australia
- Posts
- 25
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;
}
}
?>
-
12-26-2006, 10:56 PM #2Junior Guru Wannabe
- Join Date
- Dec 2004
- Posts
- 50
Cool This will be nifty for some upcoming websites of mine
-
12-29-2006, 02:17 PM #3Web Hosting Master
- Join Date
- Mar 2006
- Posts
- 984
PHP Code:# $db->connect(
# localhost,
# test_user,
# test_pass,
# test_db
# );
PHP Code:$db->connect(localhost, test_user, test_pass, test_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 #4Newbie
- Join Date
- Jan 2007
- Posts
- 5
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 #5Web Hosting Master
- Join Date
- Dec 2003
- Location
- Vancouver BC, eh?
- Posts
- 571
nik0las has a solid point. Using this object can create a problem on a very active site.
http://us3.php.net/manual/en/language.oop5.patterns.php
http://www.imarc.net/blog/118/implem...php_singleton/
http://www.patternsforphp.com/wiki/Singleton
-
07-08-2009, 01:02 PM #6New Member
- Join Date
- Jun 2009
- Location
- NY
- Posts
- 1
Thanks this helped me out
-
07-17-2009, 02:44 PM #7Newbie
- Join Date
- May 2009
- Posts
- 9
thanks alot
-
07-25-2009, 01:07 PM #8Newbie
- Join Date
- Jun 2009
- Posts
- 17
Thanks in advanced
-
08-28-2009, 11:51 AM #9WHT Addict
- Join Date
- Aug 2009
- Location
- England
- Posts
- 165
Very useful, thank you.
-
10-12-2009, 05:34 AM #10Newbie
- Join Date
- Mar 2007
- Posts
- 7
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 #11Newbie
- Join Date
- Oct 2009
- Location
- Canada
- Posts
- 18
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 #12Newbie
- Join Date
- Sep 2009
- Posts
- 7
-
11-23-2009, 12:22 AM #13Web Hosting Master
- Join Date
- Mar 2009
- Posts
- 654
-
11-28-2009, 04:41 AM #14Web Hosting Master
- Join Date
- Mar 2009
- Posts
- 2,222
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 #15Web Hosting Master
- Join Date
- Mar 2009
- Posts
- 2,222
-
11-30-2009, 12:11 AM #16New Member
- Join Date
- Nov 2009
- Posts
- 1
Connecting to multiple databases ??
Hello guys,
I've a problem with a similar script, It's a config file for a MySQL Class
I need the PHP script to query 3 databases at the same time, and I just keep failing
I need help on how to inser the 3 dbnames into this file ???
thanks in advance
this is the code::
PHP Code:<?php
class Conf {
var $smtphost;
var $dbhost;
var $dbport;
var $dbname;
var $dbuser;
var $version;
function Conf() {
$this->dbhost = 'localhost';
$this->dbport = '3306';
$this->dbname = 'sbshost_asbs';
$this->dbuser = 'sbshost_hrms';
$this->dbpass = 'openup1';
$this->version = '2.5';
$this->emailConfiguration = dirname(__FILE__).'/mailConf.php';
$this->errorLog = realpath(dirname(__FILE__).'/../logs/').'/';
}
}
?>
-
12-04-2009, 07:02 AM #17Newbie
- Join Date
- Oct 2009
- Location
- UK
- Posts
- 16
$conn=mysql_connect(SERVER_NAME,USER_NAME,PASSWORD);
$db=mysql_select_db(DATABASE_NAME);
A simple code