|
|
|
This is an example of a DBI object with PHP. This is a functional object that works well with
MySQL. This can be altered to work with ODBC, Oracle, Postgres...
You will have to change the values of the object properties and fill in the proper username,
password, etc.
<?
/********************************************************
* PHP DBI Object
*
* This object acts like Perls DBI object. To use this
* object just require this object file, and then perform
* the following steps with your statements:
*
* require 'PhpDBI.inc';
* $db = new PhpDBI;
* $sql = "select aField from tableName ";
* $db->connect();
* $db->prepare($sql);
* $db->execute($sql);
* while ($returnVar = mysql_fetch_array($db->sth)) {
* DO_SOMETHING_WITH: $returnVar['aField'];
* }
* $db->disconnect();
********************************************************/
class PhpDBI {
/* DB OBJECT PROPERTIES */
var $dbh = false; // database handler
var $sth; // statement handler (our cursor)
var $user = "USERNAME"; // db username
var $pass = "PASSWORD"; // db password
var $host = "HOSTNAME"; // hostname for database (usually localhost)
var $db = "DATABASE"; // name of database
var $sql; // sql statement
/* METHOD: Connect to Database
* returns: true on a good connection, false otherwise */
function connect() {
$this->dbh = false;
$this->dbh = mysql_connect($this->host,$this->user,$this->pass);
mysql_select_db($this->db,$this->dbh);
if (!$this->dbh)
return false;
else
return $dbh;
}
/* METHOD: Check Database connection
* returns: database handler */
function check() {
return $this->dbh;
}
/* METHOD: Disconnect from database */
function disconnect() {
mysql_close($this->dbh);
$this->dbh = false;
}
/* METHOD: Load SQL statement into object */
function prepare($sql) {
$this->sql = $sql;
}
/* METHOD: Execute a random SQL Query
* expects: SQL String
* boolean $ret ( 1 for needing a return value, 0 for no return) */
function execute($sql) {
if ($this->check())
$this->sth = mysql_query($this->sql,$this->dbh);
}
}
?> |
|
| Powerful php/mysql Pagination for up to 6 URL Params Categories : PHP, PHP Classes, Databases, MySQL, Navigation | | | Password reminder Categories : PHP, PHP Classes, Databases, MySQL, Mail | | | MySQL Class to ease Database connectivity Categories : MySQL, PHP Classes, Databases, PHP | | | usercounter class Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables | | | MySQL Handler Categories : PHP, Databases, MySQL, Classes and Objects, PHP Classes | | | PostGreSQL and MySQL 2 in 1 db Manager Categories : PHP, PHP Classes, Databases, PostgreSQL, MySQL | | | Simple Mini Poll class library (SimPoll) Categories : PHP, PHP Classes, Databases, MySQL, Complete Programs | | | Online Automatic Class Generator for MySQL Tables Categories : PHP, PHP Classes, Classes and Objects, Databases, MySQL | | | Specify your connection settings and create a link to a MySQL database. Categories : PHP, PHP Classes, Databases, MySQL, Beginner Guides | | | Simple database class Categories : PHP, PHP Classes, MySQL, Databases | | | Simple usersOnline class - keep track of how many users are online on your site Categories : PHP, PHP Classes, Databases, MySQL | | | Setting up InnoDB on MySQL and using Transactions Begin, Commit, Rollback in PHP. Categories : PHP Classes, Databases, PHP, MySQL, InnoDB | | | Ajax PHP Tree (Left and Right) with MySQL Categories : PHP, Databases, MySQL, AJAX, PHP Classes | | | YellowPages Content Grabber (PHP5 +) Categories : PHP, PHP Classes, Regexps, Databases, MySQL | | | This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server. Categories : Databases, MySQL, Complete Programs, PHP, Databases | |
|
|
|