Search
 
SCRIPT & CODE EXAMPLE
 

PHP

Php OOP function CRUD

<?php
class Database{
	private $host="localhost";
	private $username="root";
	private $password="";
	private $database="rest_api";
	private $DbCon;
	
	
	public function connect(){
		$con = new mysqli($this->host,$this->username,$this->password,$this->database);
		
		if($con){
			$this->DbCon=$con;
			return true;
		}else{
			return false;
		}
	}
	public function select($table,$row="*",$where=null,$order=null){
		$query='SELECT '.$row.' FROM '.$table;
		if($where!=null){
			$query.=' WHERE '.$where;
		}
		if($order!=null){
			$query.=' ORDER BY ';
		}
		$Result=$this->DbCon->query($query);
		return $Result;

	}
	public function insert($table,$value,$row=null){
		$insert= " INSERT INTO ".$table;
		if($row!=null){
			$insert.=" (". $row." ) ";
		}
		for($i=0; $i<count($value); $i++){
			if(is_string($value[$i])){
				$value[$i]= '"'. $value[$i] . '"';
			}
		}
		$value=implode(',',$value);
		$insert.=' VALUES ('.$value.')';
		$ins=$this->DbCon->query($insert);
		if($ins){
			return true;
		}else{
			return false;
		}
	}
	public function delete($table,$where=null){
		if($where == null)
            {
                $delete = "DELETE ".$table;
            }
            else
            {
                $delete = "DELETE  FROM ".$table." WHERE ".$where;
            }
			$del=$this->DbCon->query($delete);
			if($del){
				return true;
			}else{
				return false;
			}
	}
	public function update($table,$rows,$where){
		 // Parse the where values
            // even values (including 0) contain the where rows
            // odd values contain the clauses for the row
            for($i = 0; $i < count($where); $i++)
            {
                if($i%2 != 0)
                {
                    if(is_string($where[$i]))
                    {
                        if(($i+1) != null)
                            $where[$i] = '"'.$where[$i].'" AND ';
                        else
                            $where[$i] = '"'.$where[$i].'"';
                    }
                }
            }
            $where = implode(" ",$where);


            $update = 'UPDATE '.$table.' SET ';
            $keys = array_keys($rows);
            for($i = 0; $i < count($rows); $i++)
            {
                if(is_string($rows[$keys[$i]]))
                {
                    $update .= $keys[$i].'="'.$rows[$keys[$i]].'"';
                }
                else
                {
                    $update .= $keys[$i].'='.$rows[$keys[$i]];
                }

                // Parse to add commas
                if($i != count($rows)-1)
                {
                    $update .= ',';
                }
            }
            $update .= ' WHERE '.$where;
            $query = $this->DbCon->query($update);
            if($query)
            {
                return true;
            }
            else
            {
                return false;
            }
	    
         }
        
	
};
$a= new Database();
$a->connect();

// $upd=array(
// 		'name'=>'Badshah',
// 		'email'=>'badshah@gmail.com'
// 		);
// $a->update('user',$upd,array('id=55'));

// $a->delete('user',' id = 7');

// $ins=array('','badshah','badshah@gmail.com');
// $a->insert('user',$ins,null);

// $ab=$a->select('user');
// while($a=$ab->fetch_array()){
// 	echo $a['name']."<br />";
// }


?>
Comment

php oop crud database

-- Table structure for table `customers`
--

CREATE TABLE `customers` (
  `id` int(100) NOT NULL,
  `name` varchar(100) NOT NULL,
  `email` varchar(100) NOT NULL,
  `username` varchar(100) NOT NULL,
  `password` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Comment

PREVIOUS NEXT
Code Example
Php :: nested loop in php 
Php :: laravel crud application 
Php :: laravel get() 
Php :: create laravel migration 
Php :: laravel link to css or image 
Php :: serve https with php local 
Php :: php header 
Php :: install laravel scout 
Php :: php array_pop with key 
Php :: laravel kill current session 
Php :: php online test 
Php :: randhex php 
Php :: php fn closure 
Php :: create seed file from db laravel 
Php :: status validation laravel 
Php :: php overriding 
Php :: php ?? operator 
Php :: oop in php 
Php :: heredoc 
Php :: pagination in api laravel 
Php :: join multiple query in laravel 
Php :: php .com 
Php :: laravel Call to a member function validate() on array 
Php :: Mixed int and string keys 
Php :: laravel $browser-keys alt click 
Php :: utf8mb4 decode in php 
Php :: php artisan tinker get records 
Php :: php array associatif move element 
Php :: What does this mean in PHP: - or = 
Php :: update query not working no errors found php mysql 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =