i made a class for mysql too

, not saying mine is better then yours (or vice versa) but have a look

.
Code: Select all
<?php
class mysql
{
//
var $class_name = "cMysql";
var $class_author = "Qads";
var $class_build = "8 - 20 Dec 03";//last time it was edited
var $VERSION = "1.0";
var $regeisterd = "No";
var $rege_name = "N/a";
var $rege_org = "N/a";
//
var $record;
var $DEMO_MODE = false;
var $HOST = "localhost";
var $DB_USER = "***";
var $DB_PASS = "****";
var $DB_NAME = "dbname here";
//throw out the error;
function error($msg)
{
if($this->DEMO_MODE == true)
{
$demo = "<div class="error-title" align="center">Demo Mode</div>";
}
if(strstr($msg, "|"))
{
$msg = str_replace("[/font]","</font></b>", $msg);
$msg = str_replace("|","", $msg);
}
else
{
$msg = str_replace("[/font]","</font></b><br />", $msg);
}
$msg = str_replace("[", "<b><font color="", $msg);
$msg = str_replace("]", "">", $msg);
$msg = '<style>
.error-title{
font-family: "Courier New", Courier, monospace;
font-size: medium;
font-weight: bold;
color: Red;
padding: 4px;
}
.error-msg{
background: #F4F4F4;
border: 1px black solid;
color: Black;
padding: 10px;
font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
font-size: 9pt;
}
.version{
color: black;
font-family: "Courier New", Courier, monospace;
font-size: 8pt;
font-weight: bolder;
}
</style>
'.$demo.'
<table width="512" border="0" cellpadding="0" cellspacing="0" class="table">
<tr>
<td width="512" height="21" valign="top" class="error-title">ERROR!</td>
</tr>
<tr>
<td height="85" valign="top" class="error-msg">'.$msg.'</td>
</tr> <tr>
<td valign="top" align="right" class="version">cMysql '.$this->VERSION.' © Digitalcoder.co.uk</td>
</tr>
</table>';
return $msg;
}
//content to database
function connect()
{
@mysql_connect($this->HOST, $this->DB_USER, $this->DB_PASS)or die($this->error("[red]Unable to connect to database host![/font]Please make sure that you are useing correct host name, database username and database password."));
@mysql_select_db($this->DB_NAME)or die($this->error("[red]unable to select to database![/font]Please make sure you are useing a correct database name."));
}
//mysql fields in sql format
function format_fields($fields)
{
$explode = explode(",", $fields);
for($x = 0; $x<=count($explode); $x++)
{
if(!empty($explode[$x]))
{
$explode[$x] = trim($explode[$x]);
if($x < 1)
{
$formated = "$explode[$x]";
}
else
{
$formated .= ", $explode[$x]";
}
}
}
return $formated;
}
//
function do_query($query)
{
global $queries_done;
$result=mysql_query($query);
$queries_done++;
return $result;
}
//
function query($fields, $table, $where)
{
$this->connect();
$fields = $this->format_fields($fields);
$query = $this->do_query("SELECT $fields FROM `$table` WHERE $where")or die($this->error("[red]Invild Query![/font][blue]SELECT $fields FROM `$table` WHERE $where [/font]".mysql_error()));
return $query;
}
//
/**
* @return array
* @param $query_id mysql_query as input
* @desc takes mysql_query resourcse id
*/
function get_array($query_id)
{
$this->record = mysql_fetch_array($query_id);
return $this->record;
}
//
function num_rows($query_id)
{
$num_rows = mysql_num_rows($query_id);
return $num_rows;
}
//
function nextpage($table,$perpage,$start,$page, $query)
{
$start = (int)$start;
$query = "SELECT count(*) as count FROM $table $query";
$result = $this->do_query($query)or die($this->error("[red]Mysql Error: Invild Query![/font]SELECT count(*) as count FROM $table $query<br />".mysql_error()));
$row = $this->get_array($result);
$numrows = $row[0];
echo "<script language="JavaScript" type="text/JavaScript">
function MM_goToURL() {
var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}
</script>";
if($start > 0)
{
$r = "<input name="nextbtn" type="button" id="previousbtn" onClick="MM_goToURL('parent','".$page."&start=".($start - $perpage)."');return document.MM_returnValue" value="<<< Previous">";
}
if($numrows > ($start + $perpage))
{
$r .= " <input name="nextbtn" type="button" id="nextbtn" onClick="MM_goToURL('parent','".$page."&start=".($start + $perpage)."');return document.MM_returnValue" value="Next >>>">";
}
return $r;
}
//
function insert($table, $fields, $values)
{
$this->connect();
$into = explode(",", $fields);
$value = explode(",", $values);
if(count($value) != count($into))
{
die($this->error("[red]Insert Fields Count Mismatch![/font]Unable to insert record.<pre>Fields: ".print_r($into)."<pre/><pre>Values: ".print_r($value)."</pre>"));
}
for($x = 0; $x<count($value); $x++) // CHANGED <= TO <
{
$value[$x] = addslashes(trim($value[$x]));
$into[$x] = trim($into[$x]);
if($x == 0)
{
$fields = "`$into[$x]`";
$gen_query = "'$value[$x]'";
}
else
{
$fields .= ", `$into[$x]`";
$gen_query .= ", '$value[$x]'";
}
}
$query = $this->do_query("INSERT INTO `$table` ($fields) VALUES($gen_query)")or die($this->error("[red]Invild Query![/font]Unable to update record.<br />".mysql_error()));
}
//
function Delete($table, $where)
{
$this->connect();
$query = $this->do_query("DELETE FROM `$table` WHERE $where")or die($this->error("[red]Invild Query![/font]Unable to delete record.<br />[blue]DELETE FROM `$table` WHERE $where [/font]".mysql_error()));
}
//
function update($toupdate, $value, $table, $where)
{
$toupdate = explode(",", $toupdate);
$value = explode(",", $value);
if(count($value) != count($toupdate))
{
die($this->error("[red]Field count mismatch[/font]Unable to update record."));
}
$gen_query = "";
for($x = 0; $x<=count($value); $x++)
{
if(!empty($toupdate[$x]))
{
if($x == 0)
{
$gen_query .= "`$toupdate[$x]` = '$value[$x]'";
}
else
{
$gen_query .= ", `$toupdate[$x]` = '$value[$x]'";
}
}
}
$update = $this->do_query("UPDATE `$table` SET $gen_query WHERE $where")or die($this->error("[red]Invild Query![/font]Unable to update record.<br />[blue]UPDATE `$table` SET $gen_query WHERE $where [/font]".mysql_error()));
}
//
function unset_array($array)
{
if(is_array($array))
{
for($x = 0; $x<=count($array); $x++)
{
unset($array[$x]);
}
}
else
{
die($this->error("[red]Unable to unset array[/font]Passed value is not a array!"));
}
}
//
//
function about()
{
$html = '
<style type="text/css">
<!--
.header {
font-family: Geneva, Arial, Helvetica, sans-serif;
font-size: 10pt;
font-weight: bold;
}
.txt
{
font-family: Geneva, Arial, Helvetica, sans-serif;
font-size: 10pt;
background-color: #EEEEEE;
padding: 4px;
border: 1px black solid;
}
-->
</style>
<center>
<table width="60%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="100%" height="22" align="left" valign="bottom" class="header">'.$this->class_name.' Version Information</td>
</tr>
<tr>
<td height="19" align="left" valign="top" class="txt">Version: '.$this->VERSION.'<br />
Build: '.$this->class_build.'<br />
Author: '.$this->class_author.'</td>
</tr>
<tr>
<td height="19" align="left" valign="middle" class="header">Registration Information</td>
</tr>
<tr>
<td height="19" align="left" valign="top" class="txt">Registered: '.$this->regeisterd.'<br />
Name: '.$this->rege_name.'<br />
Organization: '.$this->rege_org.'</td>
</tr>
</table>
<hr align="center" width="50%" size="1" noshade="noshade" />
</center>';
return $html;
}
//
}
$mysql = &NEW mysql();
$mysql->connect();
?>
it is still being worked on, i have the db, host information in the class beacuse each project has only one so i dont see a need to define it on every page

....any suggestions?
exmaples...
Code: Select all
<?php
//query
$query = $mysql->query("ID,username,email", $login_table, "`username` = '$username' AND `password` = '$password' limit 1");
//num rows
$num_rows = mysql_num_rows($query);
//update
$update_session_time = $mysql->update("time", "$time", "session_data", "`session_id` = '$sess_id' AND `user_id` = '$user_id' AND `ip` = '$user_ip' LIMIT 1");
//insert
$mysql->insert("session_data", "session_id,user_id,ip,time", "$sid,$data[ID],$ip,$time");
//delete
$delete_old = $mysql->Delete("session_data", "`time` <= '$invild_sessions'");
//get array
$row = $mysql->get_array($query);
//give this a try!
echo $mysql->about();
?>
very easy to use and shows all the required errors in nice format.