<?php // i prefer short tags but really this is a source of alot of headaches
/* Please leave this comment in at the top of the script if you use this script
* in it's entirety. That is my only request. Other then that have fun.
* created by bobo (uregina -at- gmail -dot- com)
*
* I've often wondered why the database classes out there were really a little
* cumbersome to work with. For me I value simplicity and power and have been
* working on this little gem now for about 6 years with little tweaks here
* and little tweaks there. I wasn't about to pack it with a gazillion features
* but more just to kill off redundancy in my code.
*
* example usage:
*
* 1) create a file called inc.config.php in it throw this:
* // include the class:
* include_once('class.db.php');
* // set the variables:
* $db_host = "localhost";
* $db_user = "myusername";
* $db_pass = "mypassword";
* $db_name = "mydatabasename";
* // make a db object:
* $db = new mydb($db_host, $db_user, $db_pass, $db_name);
*
* 2) now in your file that you want to access the db from just include_once('inc.config.php');
* don't worry it won't connect till it's first query and it does so automagically.
*
* 3) some example usage:
* // an insert
* $SQL = "INSERT INTO addressbook (name) VALUES ('bob')";
* $insertid = $db->q($SQL); // returns false if no insertid or the id
* echo $insertid;
*
* // a single row returned
* $SQL = "SELECT * FROM addressbook WHERE name LIKE 'bob' LIMIT 1";
* $row = $db->row($SQL);
* echo $row['name'];
*
* // multiple rows returned
* $SQL = "SELECT * FROM addressbook";
* $addresses = $db->get($SQL);
* foreach($addresses AS $address)
* echo $address['name'];
*
* // prematurely open
* $db->connect();
* // prematurely close
* $db->close(); // will reopen automatically if needed again
*
*
* Also if you're looking for help from me directly you can usually find me
* slumming it on #phphelp on the undernet irc network.
* my nick on there is usually _P_H_P_ (I'm sometimes in channel just not actually at my machine)
* in that case I'd love to still hear from you at my email listed above.
*
*/
class mydb
{
var $host, $user, $pass, $db_name;
var $dlink;
function mydb($host,$user,$pass, $db)
{
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->db_name = $db;
}
function connect()
{
# Regular Connection:
$this->dlink = mysql_connect($this->host, $this->user, $this->pass) or die(mysql_error()."<BR><BR><B> :: in class db : 1");
# Persistent Connection:
//$this->dlink = mysql_pconnect($this->host, $this->user, $this->pass) or die(mysql_error()."<BR><BR><B> :: in class db : 1");
# Persistent Connection with Compression:
// $this->dlink = mysql_pconnect($this->host, $this->user, $this->pass, MYSQL_CLIENT_COMPRESS) or die(mysql_error()."<BR><BR><B> :: in class db : 1");
mysql_select_db($this->db_name) or die(mysql_error()."<BR><BR><B> :: in class db : 2");
}
function close()
{
@mysql_close($this->dlink);
$this->dlink = "";
}
function query($query)
{
if(!isset($this->dlink) || empty($this->dlink))
$this->connect();
mysql_query($query) or die(mysql_error()."<BR><PRE>$query</PRE><BR><B> :: in class db : 3");
return mysql_insert_id();
}
function query_return($query)
{
if(!isset($this->dlink) || empty($this->dlink))
$this->connect();
$tempresult = mysql_query($query) or die(mysql_error()."<BR><BR><B> :: in class db : 4");
while ($row = mysql_fetch_array($tempresult, MYSQL_ASSOC))
$result[] = $row;
mysql_free_result($tempresult);
return @$result;
}
// new aliases ...
# do a single query (returns an insert id if there is one; doesn't return SELECTS)
function q($query){ return $this->query($query);}
# generic query (returns SELECT data in an array eg: $data[0]=array('id'=>1,'name'=>'bob')
function get($query){ return $this->query_return($query); }
# this will return just the first row of returned data ($data = array('id'=>1,'name'=>'bob') )
function row($query){ if(!$DATA = $this->get($query)) return false; else return $DATA[0]; }
// yes I'm sure this one can be super duper optimized.
function safe_text($text) // will convert the special characters into html escape codes
{
$text = htmlspecialchars($text,ENT_QUOTES);
return $text;
}
function table_exists($table)
{
if(!isset($this->dlink) || empty($this->dlink))
$this->connect();
$exists = mysql_query('SHOW TABLES FROM `'.$this->db_name.'` LIKE \''.$table.'\'');
if(mysql_num_rows($exists) == 1) return true; else return false;
}
function insert_id()
{
return mysql_insert_id();
}
}
?>