Hello Users, This post is regarding the OOPS(Object Oriented Programming) concept in PHP, In this post i have show How to create class and use as a Database connection in Your Webapplication.so using this class you can easily play with your data with your web application and database

In below you can get class and use it.in below i have created one connection class and in a class i have created Insert,Update,Delete Function So You can easily way to connect with your database and use the function in your web application

/*
   This code is develop By  Massive Technolab
   [email protected]
   You can use for developing purpose
   File Name: connection.php

*/
 class connection
 {
	 
	 
	 function connect()
	 {
		 $ssServerName = "localhost";//Your server name
	     $ssUserName = "your_user_name_here";//root
		 $ssPassword = "your_password_here";//database password
	     $ssDbname = "database_name_here";//database name here
		$con = mysql_connect($ssServerName,$ssUserName,$ssPassword);
		if (!$con)
		{
			die('Could not connect: ' . mysql_error());
		}
		mysql_select_db($ssDbname,$con);
	 }
	 function update_data($ssTable,$ssField,$ssValue)
	 {
		$sql = "UPDATE  $ssTable SET $ssField = '".$ssValue."' ";
		mysql_query($sql);
	 }
	 function fetch_data($result)
	 {
		while($row = mysql_fetch_array($result))
		{
		  $arr[] = $row ;
		}
		return $arr;  
	 }
	 function select($ssTable)
	 {
		$ssQuery = "SELECT * FROM $ssTable";
		$result = mysql_query($ssQuery); 
		while($row = mysql_fetch_array($result))
		{
		  $arr[] = $row ;
		}
		return $arr;  
	 }
	 public function insert_data($ssTableName,$asFields,$amValues)
	 {
		$ssQuery = '';
		if($ssTableName == '' || $asFields == '' || $amValues == '')
			return false;
		$ssQuery = "INSERT INTO $ssTableName(" . implode(', ', $asFields) . ") ". "VALUES ('" . implode("', '",$amValues) . "')";
		mysql_query($ssQuery);
		return true;
	 }
	 function close($con)
	 {
		 mysql_close($con);
	 }
	 function selectedit($ssTable , $ssField , $ssValue)
	 {
		$ssQuery = "SELECT * FROM $ssTable where $ssField = '".$ssValue."'  ";
		$result = mysql_query($ssQuery); 
		while($row = mysql_fetch_array($result))
		{
		  $arr[] = $row ;
		}
		return $arr;  
	 }
	  function update($ssTable_name,$asUpdateFields,$ssConField,$snConFieldValue)
	{
		
		$ssQuery="UPDATE $ssTable_name SET ";
		foreach ( $asUpdateFields as $ssFieldName =--> $ssFieldValue )
			$ssQuery .=  $ssFieldName."= '".$ssFieldValue."',";

		$ssQuery = substr($ssQuery,0,strlen($ssQuery)-1);
		$ssQuery .=" WHERE $ssConField = '$snConFieldValue' ";
		mysql_query($ssQuery);
		return true;
		//return $this->amResult ;

	}
	 function delete($sql) {
        $sql = trim($sql);
        $result = mysql_query($sql);
        if (!$result) {
            return 0;
        } else {
            return $result;
        }
    }

    function deleteData($table, $where = '') {
        $sql = "Delete From " . $table;
        if ($where) {
            $sql .= " where " . $where;
        }
		$sql = trim($sql);
        $result = mysql_query($sql);
		if (!$result) {
            return 0;
        } else {
            return $result;
        }
    }
	 
 }

	
?>

In Below I have show How To Insert Data Using Connection Class

 connect();//Here is your database connection
	if(isset($_REQUEST['btnsubmit']))
	{
		$name = $_POST['txtname'];
		$subject = $_POST['txtsub'];

		$amFields = array('name','subject'); //Here your database fields
		$amValues = array($name,$dob,$edu,$desg,$exp,$subject,$file_name);//Here is your POST value
		$Ocon->insert_data("tbl_name_here",$amFields,$amValues);//Inserted data here
		}
		
	}
?>

In Below I have show How To EDIT Data Using Connection Class

 connect();//Here is your database connection
	$id = $_GET["id"];//Get id  from post
	$amResult = $amInfo = array(); 		 
	$amResult = $Ocon->selectedit("tbl_name_here","id_from_feild",$id);
	$name = $amResult[0]['name'];
	$subject = $amResult[0]['subject'];
	
?>

In Below I have show How To Update Data Using Connection Class

 connect();//Here is your database connection
	$id = $_GET["id"];//Get id  from post
	$btnUpdate = isset($_POST['btnupdate']) ? $_POST['btnupdate'] : "";
	if($btnUpdate != "")
	{
	    $name = $_POST['txtname'];
		$subject = $_POST['txtsub'];
	 	$asUpdateFields = array('name'=>$name,'subject' => $subject);
		$Ocon->update('tbl_name_here',$asUpdateFields,'id_from_tbl_feild',$id);
	}
	
?>

In Below I have show How To Delete Data Using Connection Class

 connect();//Here is your database connection
	$id = $_GET["id"];//Get id  from post
	$btnDelete = isset($_POST['btndelete']) ? $_POST['btndelete'] : "";
	if($btnDelete != "")
	{
	   $where = "id=".$id;
	   $Ocon->deleteData('tbl_name_here',$where);
	}
	
?>
 connect();//Here is your database connection
	$btnDelete = isset($_POST['btndelete']) ? $_POST['btndelete'] : "";
	if($btnDelete != "")
	{
	   $DEL_SQL = "DELETE FROM table_name";
	   $Ocon->delete($DEL_SQL);
	}
	
?>

Download

About The Massive Technolab

Avatar
Who We Are

Massive Technolab is a Leading IT services Company in India that expertise in web Development, Web Design and SEO at affordable rates.

Leave a Comment

comments powered by Disqus