Parsing mysql operations using mysqli extension library in PHP

Source: Internet
Author: User

1. Use mysqli extension library in PHP to perform dql operations on mysql
Copy codeThe Code is as follows: <? Php
Header ("Content-type: text/html; charset = UTF-8 ");
// Use mysqli to operate mysql databases (Object-Oriented)
// 1. Create a MySQLi object
$ Mysqli = new MySQLi ("localhost", "root", "root", "test ");
If ($ mysqli-> connect_error ){
Die ("connection failed". $ mysqli-> connect_error );
}
// 2. Operate the database (send SQL)
$ SQL = "select * from user1 ";
// 3. processing result
$ Res = $ mysqli-> query ($ SQL );
// Var_dump ($ res );
// Fetch_assoc \ fetch_array \ fetch_object
While ($ row = $ res-> fetch_row ()){
Var_dump ($ row );
/* Foreach ($ row as $ val ){
Echo '--'. $ val;
}
Echo '<br/> ';*/
}
// 4. Close the resource
$ Res-> free ();
$ Mysqli-> close ();
?>

The following process-orientedCopy codeThe Code is as follows: <? Php
Header ("Content-type: text/html; charset = UTF-8 ");

$ Mysqli = mysqli_connect ("localhost", "root", "root", "test ");
If (! $ Mysqli ){
Die ("connection failed". mysqli_connect_error ());
}
$ SQL = "select * from user1 ";
$ Res = mysqli_query ($ mysqli, $ SQL );
// Var_dump ($ res );
While ($ row = mysqli_fetch_row ($ res )){

Foreach ($ row as $ val ){

Echo '-'. $ val;
}
Echo '<br/> ';
}
// Release the memory
Mysqli_free_result ($ res );
Mysqli_close ($ mysqli );
?>

2. Use the mysqli extension library in PHP to perform dml operations on mysql
Copy codeThe Code is as follows: <? Php

// Use the mysqli extension library to perform crud operations on mysql
Header ("Content-type: text/html; charset = UTF-8 ");
$ Mysqli = new MySQLi ("localhost", "root", "root", "test ");
If ($ mysqli-> connect_error ){
Die ("connection failed". $ mysql-> connect_error );
}
// Add a record
// $ SQL = "insert into user1 (name, password, email, age) values ('Lucy ', md5 ('Lucy'), 'Lucy @ 163.com ', 17) ";
// Delete a record
// $ SQL = "delete from user1 where id = 80 ";
// Update a record
$ SQL = "update user1 set age = 20 where id = 7 ";
$ Res = $ mysqli-> query ($ SQL );
If (! $ Res ){
Echo "operation failed". $ mysqli-> error;
} Else {
If ($ mysqli-> affected_rows> 0 ){
Echo "successful ";
} Else {
Echo "No rows affected ";
}
}
// Close the resource
$ Mysqli-> close ();
?>

3. Encapsulation
Copy codeThe Code is as follows: <?
Class SqlHelper {

Private $ mysqli;
// Write to death first, and then use a file for configuration.
Private static $ host = "localhost ";
Private static $ user = "root ";
Private static $ pwd = "root ";
Private static $ db = "test ";
Public function _ construct (){

$ This-> mysqli = new MySQLi (self ::$ host, self ::$ user, self ::$ pwd, self ::$ db );
If ($ this-> mysqli-> connect_error ){
Die ("connection failed". $ this-> mysqli-> connect_error );
}
// Set the character set
$ This-> mysqli-> query ("set names utf8 ");
}
// Dql operate
Function execute_dql ($ SQL ){
$ Res = $ this-> mysqli-> query ($ SQL) or die ($ this-> mysqli-> error );
Return $ res;
}
// Dml operate
Function execute_dml ($ SQL ){
$ Res = $ this-> mysqli-> query ($ SQL) or die ($ this-> mysqli-> error );

If (! $ Res ){
Return 0; // failed
} Else {
If ($ this-> mysqli-> affected_rows> 0 ){
Return 1; // success
} Else {
Return 2; // No effect on rows
}
}
}
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.