MySQL Tutorial database Tutorial Connection Classes
This article provides a perfect PHP tutorial MySQL database connection class program, simple example.
*/
Class db{
var $linkid;
var $sqlid;
var $record;
function db ($host = "", $username = "", $password = "", $database = "")
{
if (! $this->linkid) @ $this->linkid = mysql_connect ($host, $username, $password) or Die ("Connection server failed.");
@mysql_select_db ($database, $this->linkid) or Die ("Unable to open database");
return $this->linkid;}
function query ($sql)
{if ($this->sqlid=mysql_query ($sql, $this->linkid)) return $this->sqlid;
else {
$this->err_report ($sql, mysql_error);
return false;}
}
function nr ($sql _id= "")
{if (! $sql _id) $sql _id= $this->sqlid;
Return mysql_num_rows ($sql _id);
function NF ($sql _id= "")
{if (! $sql _id) $sql _id= $this->sqlid;
Return Mysql_num_fields ($sql _id);
function NextRecord ($sql _id= "")
{if (! $sql _id) $sql _id= $this->sqlid;
if ($this->record=mysql_fetch_array ($sql _id)) return $this->record;
else return false;
}
function f ($name)
{
if ($this->record[$name]) return $this->record[$name];
else return false;
}
function Close () {mysql_close ($this->linkid);}
function Lock ($tblname, $op = "Write")
{if (mysql_query ("Lock Tables". $tblname. " ". $op)) return true; else return false;
function unlock ()
{if (mysql_query ("Unlock tables") return is true; else return false;}
function Ar () {
Return @mysql_affected_rows ($this->linkid);
}
function i_id () {
return mysql_insert_id ();
}
Function Err_report ($sql, $err)
{
echo "MySQL query error <br>";
Echo Query statement: ". $sql." <br> ";
Echo error message:. $err;
}