Results 1 to 17 of 17
Thread: MySQL Class
Threaded View
-
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;
}
}
?>