Hello,
My pmachine code for db.mysql.php class is if that can help:
<?php
//
// pMachine
//
// Copyright (c) 2001,2002 Rick Ellis All rights reserved.
//
// File Name: db.mysql.php
//
// Description: MySQL Database Abstraction Classes
// These are two fairly basic database classes which contain only the functions
// necessary for pMahchine. It might be necessary to add methods to these
// classes in the future, as I didn't bother with functions that are not currently
// needed.
//
/*
EXAMPLE CODE FOR DEVELOPERS
$db = new DB();
Creates a new instance of the DB class and calls the constructor. The
constructor automatically connects to the database.
This is the equivalent of db_connect(), used previously in pMachine.
$db = new DB("hostname","username","password","database_name");
Optional arguments in the event you need to query more than one database
or if you want to override the config.php settings.
$query = new DB_query($db,"select * from some_table");
Instantiates the DB_query class. The constructor automatically performs
the query.
$query->db_fetch_array();
Retrieves an array with the results of the specified query.
$query->row['body'];
Accesses a particular MySQL field from the array.
$query->db_fetch_object();
Retrieves an object with the results of the specified query. For practical
purposes, this is identical to db_fetch_array(). As long a you don't reference
query results numerically in an array, either one will produce the same results.
$query->obj->body
Accesses a specifc MySQL field from the object.
$total = $query->db_num_rows();
Equivalent of mysql_num_rows() Alternately you can use it more verbose, like this:
$query->db_num_rows();
$total = $query->num_rows;
echo $total;
$query->db_fetch_rows();
Equivalent of mysql_fetch_rows();
$query->db_result();
Equivalent mysql_result()
$query->db_free_result();
Frees the memory after a query. This is done automatically after a script runs,
so it's not necessary to use. I don't know about other RDBMS's, so I included it,
although if another database requires it, all of the queries throughout pMachine
will need to be modified.
$db->db_close();
Closes connection to MySQL. This is also done automatically after a script runs,
so it's not necessary to use. It also has no effect on a persistent connection
which pMachine uses by default. However, other databases might need the database
closed explicitly, so I've included it.
EXAMPLE
$db = new DB();
$sql = "select * from pm_members";
$result = new DB_query($db, $sql);
while ($result->db_fetch_array()) {
echo $result->row['username'];
echo "<br />";
}
OR
$db = new DB();
$query = new DB_query($db, "select body from pm_weblog where post_id = '234'");
if (!$query->result)
return error_message("Invalid query");
$query->db_fetch_object();
$body = $query->obj->body;
*/
class DB
{
var $Hostname = "localhost";
var $Username = "root";
var $Password = "";
var $Database = "pmachine";
var $Connect;
var $DBselect = 1;
var $ShowError = 0;
var $Selected;
function DB($host="", $user="", $pass="", $db="", $dbselect="")
{
global $hostname, $dbusername, $dbpassword, $dbname, $conntype, $debug;
if ($host <> "" || $hostname <> "")
{
$host <> "" ? $this->Hostname = $host : $this->Hostname = $hostname;
}
if ($user <> "" || $dbusername <> "")
{
$user <> "" ? $this->Username = $user : $this->Username = $dbusername;
}
if ($pass <> "" || $dbpassword <> "")
{
$pass <> "" ? $this->Password = $pass : $this->Password = $dbpassword;
}
if ($db <> "" || $dbname <> "")
{
$db <> "" ? $this->Database = $db : $this->Database = $dbname;
}
if ($dbselect <> "") $this->DBselect = $dbselect;
if ($debug <> "") $this->ShowError = $debug;
if ($conntype ==0)
{
$this->Connect = @mysql_connect($this->Hostname,$this->Username,$this->Password);
}
else
{
$this->Connect = @mysql_pconnect($this->Hostname,$this->Username,$this->Password);
}
if (!$this->Connect)
{
if ($this->ShowError == 1)
{
return $this->db_error("Database connection failed. Please check your config settings.");
}
return false;
}
if ($this->DBselect == 1)
{
if (!@mysql_select_db($this->Database,$this->Connect))
{
if ($this->ShowError == 1)
{
return $this->db_error("MySQL Error: ",$this->Connect);
}
return false;
}
return $this->Selected = $this->Database;
}
}
function db_select()
{
if (!@mysql_select_db($this->Database,$this->Connect))
{
if ($this->ShowError == 1)
{
return $this->db_error("MySQL Error: ",$this->Connect);
}
return false;
}
return $this->Selected = $this->Database;
}
function db_create()
{
if (!@mysql_create_db($this->Database))
{
if ($this->ShowError == 1)
{
return $this->db_error("Unable to create the database specified in your config file.");
}
return false;
}
return true;
}
function db_close()
{
mysql_close($this->Connect);
}
function db_error($message,$id="")
{
if ($id)
{
$message .= mysql_errno($id) . " " . mysql_error($id);
}
echo "<br />" . $message . "<br /><br />";
}
}
class DB_query
{
var $query = 0;
var $row = array();
var $obj;
var $res;
var $numrows;
var $rows = 0;
var $errors = 0;
var $result = false;
function DB_query($db, $sql)
{
global $debug;
if ($debug <> "") $this->errors = $debug;
if ($db->Connect <> false)
{
if (!$this->query = @mysql_query($sql))
{
if ($this->errors == 1)
{
return $this->db_query_error("MySQL Error: ",$db->Connect);
}
return $this->result;
}
}
return $this->result = true;
}
function db_fetch_row()
{
return $this->row = @mysql_fetch_row($this->query);
}
function db_fetch_array()
{
return $this->row = @mysql_fetch_array($this->query);
}
function db_fetch_object()
{
return $this->obj = @mysql_fetch_object($this->query);
}
function db_num_rows()
{
return $this->numrows = @mysql_num_rows($this->query);
}
function db_result($x=0,$y="")
{
return $this->res = @mysql_result($this->query, $x, $y);
}
function db_free_result()
{
@mysql_free_result($this->query);
}
function db_query_error($message,$id="")
{
if ($id)
{
$message .= mysql_errno($id) . " " . mysql_error($id);
}
echo "<br />" . $message . "<br /><br />";
}
}
// END
?>