I use this to quickly run mysql functions needed in my scripts. I figured before I start a new script using this class I would see if there is anything I should change or improve. Any ideas?
class DB_sql
{
//You'll never believe what this does
function connect()
{
global $dbname, $servername, $dbusername, $dbpassword;
//connect to database
$this->id_link = @mysql_connect($servername, $dbusername, $dbpassword) or die;
mysql_select_db($dbname, $this->id_link);
//check connection
if (!$this->id_link) {
$this->stop('Failed to connect to Mysql Database');
}
}
function query($string, $fetch = '0')
{
// run query, get id
$qid = mysql_query($string, $this->id_link);
//go ahead and retrieve data?
if ($fetch == 0) { return $qid; } elseif ($fetch == 1){
return $this->fetch($qid);}
else { return $this->fetch_array($qid); }
}
function fetch_array($qid) {
while($row = mysql_fetch_array($qid)){
foreach( $row AS $key => $val ){
$record[$key] = stripslashes( $val );
}
}
return $record;
}
function fetch($qid)
{
//I found this piece difficult to write
return @mysql_fetch_object($qid);
}
function num_rows($qid)
{
//Line up soldiers, we need a headcount
return @mysql_num_rows($qid);
}
function close()
{
//You're not useful to us anymore, good night
return mysql_close($this->id_link);
}
function stop($statement)
{
echo $statement;
}