It's always good to write a database abstraction layer like this. That way, in whatever web app it's used in, the functions (ex: get_row) can be used regardless of what database is being used. If there is a different database than mysql, only a few lines in this abstraction layer are needed, as opposed to having to change the functions throughout the entire web app.
I use this class extensively. Hope it could be useful for fellow PHP developer out there. Feedbacks are welcome.
Below are couple of example
/*
I will use table "users" to illustrate
user_id
user_fname
user_lname
user_address
user_city
*/
$db= new ClsDatabase_db(); $db->ClsDatabase_conn();
/******************** Example 1:
Purpose: Get all data from table users where user_id is 1, and assign them to variable */
$rowUser=$db->getRow("SELECT * FROM users WHERE user_id=1"); foreach($rowUseras$key => $value) if(!is_numeric($key))eval("\$$key=\"$value\";"); //dirty trick to assign variable /* End Example 1
The "eval" function evaluate all the field and assign $user_id,$user_fname,$user_lname, etc...with the correct value. The advantage for this method is if you have to add or delete fields in your table, you don't have to go back and change this code
******************/
/*************** Example 2: quickly check if a record exit */ if($db->getOne("users","user_fname","NAMEDOESNTEXIT")=="") echo"Doesn't exit"; /*********************************************/
/*************** Add Update record *****************/ if($_POST["pageaction"]!=""){ $arrFieldValues=array("user_id" => $_POST["user_id"], // it's ok if this variable is blank, indicate a new record "user_fname" => $_POST["user_fname"], "user_lname" => $_POST["user_lname"], "user_address" => $_POST["user_address"], "user_city" => $_POST["user_city"]); if($_POST["pageaction"]=="update") $db->update("users",$arrFieldValues,array("user_id" => $_POST["user_id"])); //update record, handling and shipping are taken care in the function else//insert data instead $id=$db->insert("users",$arrFieldValues); //$id is the new insert_id }
//DIRTY TRICK: /* if you use a consistent prefix for your form such as "user_", then you can use this trick to gather data faster, and best of all dynamically:
foreach($_POST as $key => $value)
if(strstr($key,"user_")!="") $arrFieldValues[$key]=$value;
**********************/ /**************** GET all record ******************/ $arrUsers=$db->getAllRows("SELECT user_id,user_fname FROM users"); foreach($arrUseras$u){//loop through the record $lstOption.="<option value={$u["user_id"]}>{$u["user_fname"]}</option>"; } /****************************************************/
/* DEBUG
getOne(),getRow(),getAllRows(),insert(),update() all have default debug option at the end set to 0, you can set it to 1 to view the sql statement it generated.
Thank you for posting this code.
Below are couple of example
/*
I will use table "users" to illustrate
user_id
user_fname
user_lname
user_address
user_city
*/
$db= new ClsDatabase_db();
$db->ClsDatabase_conn();
/******************** Example 1:
Purpose: Get all data from table users where user_id is 1, and assign them to variable */
$rowUser=$db->getRow("SELECT * FROM users WHERE user_id=1");
foreach($rowUser as $key => $value)
if(!is_numeric($key)) eval("\$$key=\"$value\";"); //dirty trick to assign variable
/* End Example 1
The "eval" function evaluate all the field and assign $user_id,$user_fname,$user_lname, etc...with the correct value. The advantage for this method is if you have to add or delete fields in your table, you don't have to go back and change this code
******************/
/*************** Example 2: quickly check if a record exit */
if($db->getOne("users","user_fname","NAMEDOESNTEXIT")=="")
echo "Doesn't exit";
/*********************************************/
/*************** Add Update record *****************/
if($_POST["pageaction"]!=""){
$arrFieldValues=array("user_id" => $_POST["user_id"], // it's ok if this variable is blank, indicate a new record
"user_fname" => $_POST["user_fname"],
"user_lname" => $_POST["user_lname"],
"user_address" => $_POST["user_address"],
"user_city" => $_POST["user_city"]);
if($_POST["pageaction"]=="update")
$db->update("users",$arrFieldValues,array("user_id" => $_POST["user_id"])); //update record, handling and shipping are taken care in the function
else //insert data instead
$id=$db->insert("users",$arrFieldValues); //$id is the new insert_id
}
//DIRTY TRICK:
/* if you use a consistent prefix for your form such as "user_", then you can use this trick to gather data faster, and best of all dynamically:
foreach($_POST as $key => $value)
if(strstr($key,"user_")!="") $arrFieldValues[$key]=$value;
**********************/
/**************** GET all record ******************/
$arrUsers=$db->getAllRows("SELECT user_id,user_fname FROM users");
foreach($arrUser as $u){ //loop through the record
$lstOption.="<option value={$u["user_id"]}>{$u["user_fname"]}</option>";
}
/****************************************************/
/* DEBUG
getOne(),getRow(),getAllRows(),insert(),update() all have default debug option at the end set to 0, you can set it to 1 to view the sql statement it generated.
getOne($sql,1); getAllRows($sql,1);insert($table,$arrFieldValues,1); etc...
You can also use print_r to view the array generated by getRow() and getAllRows()
***********/
$ClsDatabase_die();