Posted: Tue Jul 25, 2006 5:27 pm
This is my registry... also locates class file and loads it automatically... viewtopic.php?p=287023񆄯
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Well, since you're already using eval() to get your objects I guess it should be easy to accomodate singletons, providing you stick to the same static method name to fetch your singleton (i.e. getInstance() or getSingleton() ).The Ninja Space Goat wrote:This is my registry... also locates class file and loads it automatically... viewtopic.php?p=287023񆄯
"really simple" ? Okie dokie this is the definition of the word simple; this is one I used when knocking a website up for a friend. Didn't need to do anything magic, it was just something I rushed out. Pretty generic really.The Ninja Space Goat wrote:If anybody has a really simple mysql connect & query class or set of classes, feel free to post it!
Code: Select all
<?php
class db
{
private $conn;
public function __construct()
{
//
}
public function connect($host, $user, $pass)
{
$this->conn = mysql_connect($host, $user, $pass) or die(mysql_error());
}
public function selectDb($name)
{
mysql_select_db($name, $this->conn) or die(mysql_error());
}
public function query($query)
{
$result = mysql_query($query, $this->conn) or die(mysql_error());
return $result;
}
public function fetchRow($result)
{
$row = mysql_fetch_object($result);
return $row;
}
public function disconnect()
{
mysql_close($this->conn);
}
public function escape($text)
{
return mysql_escape_string($text);
}
public function numRows($result)
{
return mysql_num_rows($result);
}
public function getLastInsertId()
{
return mysql_insert_id($this->conn);
}
}
?>Not really off topic. I have the query() method return a RecordSet for a couple of reasons. One is another difference from what was posted above is that my RecordSet contains numRows() and numCols() methods (rather than in the base class). Another reason is that I prefer to always get an object from the query() method that can report an error, so RecordSet has error methods as well. This allows it to be passed around as an autonomous object.Jenk wrote:I don't wish to drag this off topic, but aborint - RecordSet object: does it do much more than just use an internal array, with methods like "next()" "hasNext()" etc, or is there more to it? I'm yet to see a convincing implementation of the RecordSet object within PHP
edit: or do you mean, for example, $obj->row->col?
Wow I'd never thought of doing that. I'm gonna have a play.... might post the code and see if it's anything like what you mean...arborint wrote:And I have the query() function return a RecordSet object.
So I had a play. I'm not overly sure how useful this is but it was a project in any case. One good thing is the ease with which you can iterate through the resultset, jump to points in the set and/or go backwards through it. You get the insert_id by default if you run an insert query too:d11wtq wrote:Wow I'd never thought of doing that. I'm gonna have a play.... might post the code and see if it's anything like what you mean...arborint wrote:And I have the query() function return a RecordSet object.
Code: Select all
<?php
/**
* A basic DB connection class returning
* resultset objects following an iterator pattern
* @author d11wtq
*/
class DB
{
/**
* The database connection resource
* @var resource db
*/
private $conn;
/**
* The database name itself
* @var string database
*/
private $db;
/**
* An instance of a singleton
* @var object DB
*/
private static $instance = null;
/**
* Constructor
* @param string server
* @param string username
* @param string password
* @param string db name
*/
public function __construct($host, $user, $pass, $db=false)
{
$this->connect($host, $user, $pass);
if ($this->conn && $db) $this->selectDb($db);
}
/**
* Used for retreiving an instance of a singleton if wanted
* @return object DB
*/
public static function getInstance($host, $user, $pass, $db)
{
if (self::$instance === null)
{
self::$instance = new DB($host, $user, $pass, $db);
}
return self::$instance;
}
/**
* Connect to database (stored internally)
* @param string server
* @param string username
* @param string password
*/
public function connect($host, $user, $pass)
{
$this->conn = @mysql_connect($host, $user, $pass);
}
/**
* Change databases
* @param string database
*/
public function selectDb($db)
{
@mysql_select_db($db, $this->conn);
$this->db = $db;
}
/**
* Check which db is currently used
* @return string database
*/
public function getDbName()
{
return $this->db;
}
/**
* Check if the connection is successful
* @return boolean
*/
public function isConnected()
{
return is_resource($this->conn);
}
/**
* Close the connection
*/
public function disconnect()
{
@mysql_close($this->conn);
}
/**
* Fetch the last error
* @return string error
*/
public function getError()
{
return mysql_error($this->conn);
}
/**
* Run a query against the database and return
* a resultset iterator object
* @return object DB_Result
*/
public function query($query)
{
$result = @mysql_query($query);
$insert = false;
if (strpos(trim(strtolower($query)), 'insert') === 0) $insert = true;
return new DB_Result($result, $this->conn, $insert);
}
/**
* Retreive info about the server
* @return string info
*/
public function info()
{
return mysql_get_server_info($this->conn);
}
/**
* Get details about the current system status
* @return array details
*/
public function status()
{
return explode(' ', mysql_stat($this->conn));
}
/**
* Escape a string to make it safe for mysql
* @return string escaped output
*/
public function escape($string)
{
return mysql_real_escape_string($string, $this->conn);
}
}
?>Code: Select all
<?php
/**
* DB_Result class. Provides an iterator wrapper
* for working with a MySQL result.
* @author d11wtq
*/
class DB_Result
{
/**
* The ID that was created as a result
* of inserting a row
* @var int id
*/
private $id;
/**
* The size of the resultset
* @var int length (num rows)
*/
private $length = 0;
/**
* The result itself
* @var result result
*/
private $result;
/**
* The row at our current position in the
* resultset
* @var array row
*/
private $currentRow = array();
/**
* Current position
* @var int position
*/
private $position = 0;
/**
* The last position we were at when we read from the resultset
* @var int last position
*/
private $lastPosition = 0;
/**
* If we have pulled out any rows or not yet
* @var boolean Got rows
*/
private $gotResult = false;
/**
* The affected number of rows from the query
* @var int num rows
*/
private $affectedRows = -1;
/**
* Constructor
* @param result result
* @param resource connection
* @param boolean insert query
*/
public function __construct(&$result, &$conn, $insert=false)
{
$this->result = $result;
$this->conn = $conn;
if ((@mysql_num_rows($this->result) >= 0 && $this->result !== false) || $insert)
{
if ($insert) $this->id = mysql_insert_id($conn);
$this->length = (int) @mysql_num_rows($this->result);
$this->affectedRows = mysql_affected_rows($conn);
}
}
/**
* Magic overloaded method.
* Returns data from the resultset
* @param string column
*/
public function __get($field)
{
if ($this->lastPosition != $this->position || !$this->gotResult)
{
mysql_data_seek($this->result, $this->position);
$this->currentRow = mysql_fetch_assoc($this->result);
$this->lastPosition = $this->position;
$this->gotResult = true;
}
return $this->currentRow[$field];
}
/**
* Get the insert id
*/
public function id()
{
return $this->id;
}
/**
* Size of the resultset
*/
public function length()
{
return $this->length;
}
/**
* Go to the first row of the resultset
* @return boolean
*/
public function first()
{
if ($this->length > 0)
{
$this->goto(0);
return true;
}
else return false;
}
/**
* Go to the last row of the resultset
* @return boolean
*/
public function last()
{
return $this->goto($this->length-1);
}
/**
* Check if we've reched the end of the resultset
* @return boolean
*/
public function end()
{
if ($this->position >= $this->length) return true;
else return false;
}
/**
* Check if we're at the start of the resultset
* @return boolean
*/
public function start()
{
return ($this->position < 0);
}
/**
* Move to the next row of the resultset
* @return boolean
*/
public function next()
{
return $this->goto($this->position+1);
}
/**
* Move to the previous row in the resultset
* @return boolean
*/
public function prev()
{
return $this->goto($this->position-1);
}
/**
* Go to a specified row in the resultset
* Row numbering starts at zero
* @param int row
* @return boolean
*/
public function goto($position)
{
if ($position < -1 || $position > $this->length) return false;
else
{
$this->position = $position;
return true;
}
}
/**
* Get the affected number of rows
*/
public function affectedRows()
{
return $this->affectedRows;
}
/**
* Get the result resource itself
*/
public function &get()
{
return $this->result;
}
/**
* Get the current position
*/
public function position()
{
return $this->position;
}
}
?>Code: Select all
<?php
require_once('DB.php');
require_once('DB_Result.php');
$db = new DB('localhost', 'user', 'pass', 'db_test');
/*
create table foo
(
id int auto_increment primary key,
one varchar(255),
two varchar(255)
)
*/
$result1 = $db->query("select * from foo");
//Go forwards through the resultset
for ($result1->first(); !$result1->end(); $result1->next())
{
echo $result1->one.' '.$result1->two.'<br />';
}
$result2 = $db->query("insert into foo (one, two) values (zip, button)");
echo $result2->id(); //The ID of the record we just added
//Go backwards through the resultset
for ($result1->last(); !$result1->start(); $result1->prev())
{
echo $result1->one.' '.$result1->two.'<br />';
}
//Get the number of rows
echo $result1->length();
//Jump to a certain row
$result1->goto(1);
echo $result1->one;
?>Sure... do as you pleaseThe Ninja Space Goat wrote:I like that very much d11... may I base my code on that?
Code: Select all
if (strpos(trim(strtolower($query)), 'insert') === 0) $insert = true;Code: Select all
if (stripos(trim($query), 'insert') === 0) $insert = true;mysql_data_seek() is only run once per iteration which is what the conditional is for in __get(). I knwo it's not very clear what that condition is for so I should comment itarborint wrote:Similar to my class d11, but yours has more stuff I think. The one thing I would change is to not do calls in the Result to mysql_insert_id(), mysql_num_rows(), mysql_affected_rows(), and mysql_data_seek() unless necessary. For most record fetching they are not necessary.