OOPs Tutorials
- What is OOPs
- Access Modifier
- Constructor
- Destructor
- Polymorphism
- Inheritence
- Method Overriding
- Abstract class
- Interface
- Traits
- This keyword
- static keyword
- Final keyword
- Class Constant
- Exception Handling
- Binding in php
- tostring in php
- Object Iteration
- Reflection
- Magic methods
- Namespaces
- CRUD Function
- OOPs CRUD
- CRUD Example
Important Links
Crud function in php
File Name :
<?php
date_default_timezone_set('Asia/Calcutta');
class funtion_lib
{
function dbRowInsert($table_name, $form_data) {
$fields = array_keys($form_data);
$sql = "INSERT INTO ".$table_name."(".implode(',', $fields).") VALUES('".implode("','", str_replace("'","\'",$form_data))."')";
if(mysql_query($sql)) {
return mysql_insert_id();
}
else{
return 0;
}
}
function dbRowUpdate($table_name, $form_data, $where_clause='') {
$whereSQL = '';
if(!empty($where_clause))
{
if(substr(strtoupper(trim($where_clause)), 0, 5) != 'WHERE'){
$whereSQL = " WHERE ".$where_clause;
}
else{
$whereSQL = " ".trim($where_clause);
}
}
$sql = "UPDATE ".$table_name." SET ";
$sets = array();
foreach($form_data as $column => $value){
$value = str_replace("'","\'",$value);
$sets[] = "`".$column."` = '".$value."'";
}
$sql .= implode(', ', $sets);
$sql .= $whereSQL;
if(mysql_query($sql)) {
return 1;
}
else{
return 0;
}
}
function dbRowDelete($table_name, $where_clause='') {
$whereSQL = '';
if(!empty($where_clause))
{
if(substr(strtoupper(trim($where_clause)), 0, 5) != 'WHERE'){
$whereSQL = " WHERE ".$where_clause;
}
else{
$whereSQL = " ".trim($where_clause);
}
}
$sql = "DELETE FROM ".$table_name.$whereSQL;
if(mysql_query($sql)) {
return 1;
}
else{
return 0;
}
}
function validate_email($email) {
$pattern = '/^[A-z0-9_\-.]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z.]{1,4}$/';
if(preg_match($pattern, $email)){
return 1;
}
else{
return 0;
}
}
function gettableRowWhereClause($table,$WhereClause){
$sql = mysql_query("select * from $table where $WhereClause ");
if(mysql_num_rows($sql)>0){
$row= mysql_fetch_assoc($sql);
return $row;
}
else{
return 0;
}
}
function countTableRowWhereClause($table,$whereClause){
$sql =mysql_query("select * from $table where $whereClause ");
if(mysql_num_rows($sql)>0){
return mysql_num_rows($sql);
}
else{
return 0;
}
}
}// class closed
$mydb = new funtion_lib();
$formData = array('test'=>$_POST['test'],'test2'=>1);
$mydb->dbRowInsert($table_name,$formData);
$mydb->dbRowUpdate($table_name,$formData,$where_clause);
$mydb->dbRowDelete($table_name,$where_clause);
$getData=$mydb->gettableRowWhereClause($table_name,$whereClause);
extract($getData);
$mydb->countTableRowWhereClause($table_name,$whereClause);
?>
File Name :
File Name :
File Name :
File Name :
File Name :