Now we learn about common function that are used in various database queries like as connecting database, create, select, update, delete.
Let's start to make all these function in php file say, mysql.php
mysql.php
<?php
class db_sql
{
var $totrec;
var $recid;
var $fetch_rec;
var $m_query;
var $query;
var $row;
//connection function
function db_connect($server = DB_SERVER, $username = DB_SERVER_USERNAME, $password = DB_SERVER_PASSWORD, $database = DB_DATABASE, $link = 'db_link')
{
global $$link;
$$link =mysql_connect($server, $username, $password);
if($$link)
{
$$link=mysql_select_db($database);
if(!$$link)
{
$msg="Can't connect from Database";
$this->db_error($msg,mysql_errno(),mysql_error());
}
}
else
{
$msg="Can't connect to MYSQL Server";
$this->db_error($msg,mysql_errno(),mysql_error());
}
return $$link;
}
//database close function
function db_close($link = 'db_link')
{
global $$link;
return mysql_close($$link);
}
// Function to print database error message, number and query
function db_error($query, $errno, $error)
{
die($errno . $error . $query);
}
// Insert query function
function db_insert($table,$data,$condition='', $link = 'db_link')
{
reset($data);
$query = 'insert into ' . $table . ' (';
while( list($columns, ) = each($data) )
{
$query .= $columns . ', ';
}
$query = substr($query, 0, -2) . ') values (';
reset($data);
while (list(, $value) = each($data))
{
switch ((string)$value)
{
case 'now()':
$query .= 'now(), ';
break;
case 'null':
$query .= 'null, ';
break;
default:
$query .= '\'' .$value. '\', ';
break;
}
}
$query = substr($query, 0, -2) . ')';
$this->db_query($query);
$this->recid=mysql_insert_id();
return $this->recid;
}
// Update query function
function db_update($table,$data,$condition = '', $link = 'db_link')
{
$query = 'update ' . $table . ' set ';
while (list($columns, $value) = each($data))
{
if($columns=='inccredits')
{
$query .= 'credits'. ' ='.$value.', ';
continue;
}
switch ((string)$value)
{
case 'now()':
$query .= $columns . ' = now(), ';
break;
case 'null':
$query .= $columns .= ' = null, ';
break;
default:
$query .= $columns . ' = \'' .$value . '\', ';
break;
}
}
$query = substr($query, 0, -2) . ' where ' . $condition;
$this->db_query($query);
return $this->m_qry;
}
// Insert Update query function
function insert_update($tblname,$arrfield,$action=" insert into ",$wherecondition="")
{
if (is_array($arrfield))
{
while(list($column,$value) = each($arrfield))
{
@$query.= ",".$column."='".$value."'";
}
}
$query1 = substr($query,1);
$sql = "$action $tblname set $query1 $wherecondition";
$res = $this->db_query($sql);
$insertid = mysql_insert_id();
return $insertid;
}
//Delete record function
function db_delete($table,$condition = '', $link = 'db_link')
{
$query='Delete from '. $table .' where '. $condition;
$this->db_query($query);
return $this->m_qry;
}
//database common query function
function db_query($query)
{
$this->query=$query;
$this->m_qry=mysql_query($this->query) or
$this->db_error($query,mysql_errno(),mysql_error());
}
//database fetch record
function db_num_rows()
{
return $this->totrec=mysql_num_rows($this->m_qry);
}
//database record fetch function
function db_fetch_array()
{
return mysql_fetch_array($this->m_qry, MYSQL_ASSOC);
}
}
//making object
$obj1= new db_sql();
$obj2= new db_sql();
$obj4= new db_sql();
$obj6= new db_sql();
$obj1->db_connect();
?>
Now let's create index.php file that contains above mysql.php file.
Comments
Post a Comment