====================MySQL=====================
<?PHPclass MySQL{ Private $mysqli; Private $result; /** * Database connection * @param $config configuration Array*/ Public functionConnect$config) { $host=$config[' Host '];//Host Address $username=$config[' username '];//User name $password=$config[' Password '];//Password $database=$config[' Database '];//Database $port=$config[' Port '];//Port number $this->mysqli =NewMysqli ($host,$username,$password,$database,$port); } /** Data query * @param $table data sheet * @param null $field field * @param null $where condition * @return Mixed Query result number Mesh*/ Public functionSelect$table,$field=NULL,$where=NULL) { $sql= "Select * FROM {$table}"; if(!Empty($field)) { $field= "".implode("', '",$field) . '; $sql=Str_replace(‘*‘,$field,$sql); } if(!Empty($where)) { $sql=$sql. ' WHERE '.$where; } $this->result =$this->mysqli->query ($sql); return $this->result->num_rows; } /** * @return mixed get all results*/ Public functionFetchall () {return $this->result->Fetch_all (MYSQLI_ASSOC); } /** * Insert data * @param $table Data Sheet * @param $data data array * @return mixed Insert ID*/ Public functionInsert$table,$data) { foreach($data as $key=$value) { $data[$key] =$this->mysqli->real_escape_string ($value); } $keys= "".implode("', '",Array_keys($data)) . '; $values= ' \ '.implode("‘,‘",array_values($data)) . ‘\‘‘; $sql= "INSERT into {$table}( {$keys}) VALUES ({$values} )"; $this->mysqli->query ($sql); return $this->mysqli->insert_id; } /** * Update data * @param $table Data Sheet * @param $data data Array * @param $where filter conditions * @return mixed affected records /c11>*/ Public functionUpdate$table,$data,$where) { foreach($data as $key=$value) { $data[$key] =$this->mysqli->real_escape_string ($value); } $sets=Array(); foreach($data as $key=$value) { $kstr= "".$key. '; $vstr= ' \ '.$value. ‘\‘‘; Array_push($sets,$kstr. ' = '.$vstr); } $kav=implode(‘,‘,$sets); $sql= "UPDATE {$table} SET {$kav} WHERE {$where}"; $this->mysqli->query ($sql); return $this->mysqli->affected_rows; } /** * Delete data * @param $table Data Sheet * @param $where filter conditions * @return mixed affected Records*/ Public functionDelete$table,$where) { $sql= "DELETE from {$table} WHERE {$where}"; $this->mysqli->query ($sql); return $this->mysqli->affected_rows; }}
mysql.class.php
==================== How to use =====================
<?PHPrequire_once' Mysql.class.php ';/*Configuring Connection Parameters*/$config=Array( ' Type ' = ' mysql ', ' host ' = ' localhost ', ' username ' = ' woider ', ' password ' = ' 3243 ', ' database ' = ' php ', ' port ' = ' 3306 ');/*connecting to a database*/$mysql=New MySQL();$mysql->connect ($config);/*Querying Data*///1. Query all data$table= ' mysqli ';//Data Sheet$num=$mysql->select ($table);Echo' Total query to '.$num. ' Bar data ';Print_r($mysql-Fetchall ());//2, query part of the data$field=Array(' username ', ' password ');//filter Fields$where= ' id% 2 = 0 ';//Filter Conditions$mysql->select ($table,$field,$where);Print_r($mysql-Fetchall ());/*Inserting Data*/$table= ' mysqli ';//Data Sheet$data=Array(//Data Array' Username ' = ' admin ', ' password ' =SHA1(' admin '));$id=$mysql->insert ($table,$data);Echo' Insert record ID is '.$id;/*Modifying Data*/$table= ' mysqli ';//Data Sheet$data=Array( ' Password ' =SHA1(' Nimda '));$where= ' id = 44 ';$rows=$mysql->update ($table,$data,$where);Echo' Number of records affected '.$rows. Reviews;/*Delete Data*/$table= ' mysqli ';$where= ' id = 45 ';$rows=$mysql->delete ($table,$where);Echo' Deleted '.$rows. ' Article data ';
PHP Database Connection Tool Class (Mysqli function Wrapper)