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 :: get action name in yii2 
Php :: learn php basic 
Php :: how to increment date in php 
Php :: psr/log is locked to version 2.0.0 and an update of this package was not requested. - psr/log 2.0.0 requires php =8.0.0 - your php version (7.4.26) does not satisfy that requirement. 
Php :: recorrer un array php 
Php :: Syntax error or access violation: 1055 
Php :: User::factory()-create( 
Php :: php forech pdo 
Php :: howw to see if a value is posted laravel 
Java :: vm options javafx 
Java :: how to detect operating system in java 
Java :: java get class by string 
Java :: The package java.awt.event is not accessible 
Java :: java save file dialog 
Java :: javafx button color 
Java :: generate random password in java 
Java :: java how to print an array 
Java :: full screen android java 
Java :: java selenium new empty tab 
Java :: datentypen java 
Java :: get tfidf score for a sentence 
Java :: java remove non numbers from string 
Java :: number of lines in file java 
Java :: take a value from keyboard java 
Java :: javafx listview get item index 
Java :: java servlets session call 
Java :: fullscreen in java 
Java :: duck number in java 
Java :: A horizontal line must be drawn through the diamond, centered vertically. • The message must be printed just above the line. • The message must be horizontally centered within the applet 
Java :: heroku java 17 enable preview 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =