<?php
require_once("dbConfig.php");
/*get table and form data
$tblName="tbl_data";
$arr=array("col1"=>"Hi", "col2"=>"Pradeep", "col3"=>"Mani");
*/
/*SELECT*/
// again where clause is left optional
function dbSELECT($con,$tblName, $select='*', $where_clause='')
{
// check for optional where clause
$whereSQL = '';
if(!empty($where_clause))
{
// check to see if the 'where' keyword exists
if(substr(strtoupper(trim($where_clause)), 0, 5) != 'WHERE')
{
// not found, add key word
$whereSQL = " WHERE ".$where_clause;
} else
{
$whereSQL = " ".trim($where_clause);
}
}
// start the actual SQL statement
$sql = "SELECT ".$select." FROM ".$tblName." ";
// append the where statement
$sql .= $whereSQL;
// run and return the query result
return mysql_query($sql);
}
function dbRowInsert($con, $tblName, $arr){
//this function for the dynamic row insertion, query build & execution
$q="";$p="";
//keys represent table columns, values represent table values
foreach($arr as $k=>$v) { $q.=$k.","; $p.="'".mysqli_real_escape_string($con,$v)."', "; }
$q = substr(trim($q), 0, -1);
$p = substr(trim($p), 0, -1);
$sql="INSERT INTO $tblName(".$q.") VALUES(".$p.")";
//mysqli_query($con,$sql);
}
function dbRowUpdate($con, $tblName, $arr, $where_clause='')
{
//check for optional where clause
$whereSQL = '';
if(!empty($where_clause))
{
//check to see if the 'where' keyword exists
if(substr(strtoupper(trim($where_clause)),0,5)!= 'WHERE')
{
//not found, add WHERE key word
$whereSQL=" WHERE ".$where_clause;
}
else
{
$whereSQL= " ".trim($where_clause);
}
//start the actual SQL statement
$sql="UPDATE ".$tblName." SET ";
//loop and build the column/
$sets=array();
foreach($arr as $k=>$v)
{
$sets[]="'".$column."' = '".$value."'";
}
$sql.=implode(', ', $sets);
//append the where statement
$sql.=$whereSQL;
//run and return the query result
return mysqli_query($con,$sql);
}
}
// the where clause is left optional incase the user wants to delete every row!
function dbRowDelete($con, $tblName, $where_clause='')
{
// check for optional where clause
$whereSQL = '';
if(!empty($where_clause))
{
// check to see if the 'where' keyword exists
if(substr(strtoupper(trim($where_clause)), 0, 5) != 'WHERE')
{
// not found, add keyword
$whereSQL = " WHERE ".$where_clause;
} else
{
$whereSQL = " ".trim($where_clause);
}
}
// build the query
$sql = "DELETE FROM ".$tblName.$whereSQL;
// run and return the query result resource
return mysql_query($sql);
}
//sample examples
/* dbSelect($con, $tblName, $select, "WHERE fecha = '$fecha'");
dbRowInsert($con, $tblName, $arr);
dbRowUpdate($con, $tblName, $arr, "WHERE id = '$id'");
dbRowDelete($con, $tblName, "WHERE id = '$id'"); */
?>
require_once("dbConfig.php");
/*get table and form data
$tblName="tbl_data";
$arr=array("col1"=>"Hi", "col2"=>"Pradeep", "col3"=>"Mani");
*/
/*SELECT*/
// again where clause is left optional
function dbSELECT($con,$tblName, $select='*', $where_clause='')
{
// check for optional where clause
$whereSQL = '';
if(!empty($where_clause))
{
// check to see if the 'where' keyword exists
if(substr(strtoupper(trim($where_clause)), 0, 5) != 'WHERE')
{
// not found, add key word
$whereSQL = " WHERE ".$where_clause;
} else
{
$whereSQL = " ".trim($where_clause);
}
}
// start the actual SQL statement
$sql = "SELECT ".$select." FROM ".$tblName." ";
// append the where statement
$sql .= $whereSQL;
// run and return the query result
return mysql_query($sql);
}
function dbRowInsert($con, $tblName, $arr){
//this function for the dynamic row insertion, query build & execution
$q="";$p="";
//keys represent table columns, values represent table values
foreach($arr as $k=>$v) { $q.=$k.","; $p.="'".mysqli_real_escape_string($con,$v)."', "; }
$q = substr(trim($q), 0, -1);
$p = substr(trim($p), 0, -1);
$sql="INSERT INTO $tblName(".$q.") VALUES(".$p.")";
//mysqli_query($con,$sql);
}
function dbRowUpdate($con, $tblName, $arr, $where_clause='')
{
//check for optional where clause
$whereSQL = '';
if(!empty($where_clause))
{
//check to see if the 'where' keyword exists
if(substr(strtoupper(trim($where_clause)),0,5)!= 'WHERE')
{
//not found, add WHERE key word
$whereSQL=" WHERE ".$where_clause;
}
else
{
$whereSQL= " ".trim($where_clause);
}
//start the actual SQL statement
$sql="UPDATE ".$tblName." SET ";
//loop and build the column/
$sets=array();
foreach($arr as $k=>$v)
{
$sets[]="'".$column."' = '".$value."'";
}
$sql.=implode(', ', $sets);
//append the where statement
$sql.=$whereSQL;
//run and return the query result
return mysqli_query($con,$sql);
}
}
// the where clause is left optional incase the user wants to delete every row!
function dbRowDelete($con, $tblName, $where_clause='')
{
// check for optional where clause
$whereSQL = '';
if(!empty($where_clause))
{
// check to see if the 'where' keyword exists
if(substr(strtoupper(trim($where_clause)), 0, 5) != 'WHERE')
{
// not found, add keyword
$whereSQL = " WHERE ".$where_clause;
} else
{
$whereSQL = " ".trim($where_clause);
}
}
// build the query
$sql = "DELETE FROM ".$tblName.$whereSQL;
// run and return the query result resource
return mysql_query($sql);
}
//sample examples
/* dbSelect($con, $tblName, $select, "WHERE fecha = '$fecha'");
dbRowInsert($con, $tblName, $arr);
dbRowUpdate($con, $tblName, $arr, "WHERE id = '$id'");
dbRowDelete($con, $tblName, "WHERE id = '$id'"); */
?>
0 comments:
Post a Comment