<?PHPclassMysqlcontrollerextendscontrollerbase{ Public $config=Array(); Public $mysql=NULL; Public functionInitialize () {$this->config =Array( ' Host ' = ' localhost:3306 ', ' username ' = ' daokrdb ', ' password ' = ' 123456 ', ' dbname ' = > ' Daokrdb ', ); } Public functionindexaction () {//Create a connection $mysql=NewMysqli ($this->config[' Host '],$this->config[' username '),$this->config[' password '),$this->config[' dbname ']); /*Change db to World db*/ //$mysqli->select_db ("World");//You can use select_db to change the database//Check connection if($mysql-connect_error) { die("Connection failed:".)$mysql-connect_error); }Else{ $thread _id=$mysql-thread_id; Echo"MySQL host information:".$mysql->host_info. " <br/> "; Echo"MySQL View server information:".$mysql->server_info. " <br/> "; Echo"MySQL gets the last database query information:".$mysql->info. " <br/> "; Echo"MySQL gets the current system state:".$mysql-Stat()." <br/> "; Echo"MySQL is thread safe:".$mysql->thread_safe (). " <br> "; Echo"MySQL returns the thread ID of the current connection:".$thread _id." <br> "; $this-MySQL=$mysql; } /*set the client character set*/ if(!$mysql->set_charset ("UTF8")) { Echo("Set UTF8 Error:".)$mysql->error. " <br> "); } Else { Echo("Currently set character sets:".)$mysql->character_set_name (). " <br> "); } /*Kill current Connection thread*/ //$mysql->kill ($thread _id); $this-Insert (); $this-Select (); $this-find (); $this-Delete (); $this-update (); $mysql-Close (); } functionSelect () {$sql= "Select t.* from T_users T"; $result=$this-MySQL->query ($sql); if($result->num_rows>0){ while($row=$result-Fetch_assoc ()) { Echo"Number:".$row[' ID ']. " Title: ".$row[' title ']. <br> "; } $result-Close (); } } //Increase functionInsert () {$sql= "INSERT into T_users (title, author,submission_date) VALUES (' John ', ' Doe ', Now ())"; if($this-MySQL->query ($sql) ===TRUE) { Echo"New record insertion success <br/>"; } Else { Echo"Error:".$sql. "<br>".$this-MySQL-error; } $name= "Learn PHP"; $query= "Select title from T_users WHERE title=?"; $binduser=$this-MySQL->prepare ($query); if($binduser){ $binduser->bind_param (' s ',$name);//i-integer (integer) d-double (double-precision floating-point) s-string (string) b-blob (Binary large object: Binary Large Objects) $binduser-execute (); $binduser->bind_result ($title); $binduser-fetch (); Echo"Bind_result Query succeeded:".$title." <br/> "; $binduser-Close (); } } //Delete functionDelete () {$sql= ' Delete from t_users where title= ' d ' '; $result=$this-MySQL->query ($sql); if($result){ Echo"Delete Success <br>"; }Else{ Echo"Error:".$sql. "<br>".$this-MySQL-error; } } //Change functionUpdate () {$sql= ' Update t_users set title= ' PHP webs ' where id=31 '; $result=$this-MySQL->query ($sql); if($result){ Echo"Update Success <br>"; }Else{ Echo"Error:".$sql. "<br>".$this-MySQL-error; } } }
Summary of Mysqli Method test