Parsing the operation of MySQL using the mysqli extension Library in PHP _php tips

Source: Internet
Author: User
1, in PHP using the mysqli extension library to the MySQL DQL operation
Copy Code code as follows:

<?php
Header ("Content-type:text/html;charset=utf-8");
MYSQLI operating the MySQL database (object-oriented mode)
1. Create Mysqli objects
$mysqli =new mysqli ("localhost", "root", "root", "test");
if ($mysqli->connect_error) {
Die ("Connection failed". $mysqli->connect_error);
}
2. Operation database (send SQL)
$sql = "Select *from user1";
3, processing results
$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 Resources
$res->free ();
$mysqli->close ();
?>

The following is a process-oriented
Copy Code code 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/> ';
}
Free memory
Mysqli_free_result ($res);
Mysqli_close ($MYSQLI);
?>

2, in PHP using the mysqli extension library for MySQL DML operation
Copy Code code as follows:

<?php

CRUD operations on MySQL using the mysqli extension library
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 "Success";
}else{
echo "No rows affected";
}
}
Close Resource
$mysqli->close ();
?>

3, carry out the encapsulation
Copy Code code as follows:

?
Class sqlhelper{

Private $mysqli;
It's written in the first place, and the thing that's written later is configured with a file.
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);
}
Setting 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;//Successful
}else{
Return 2;//no line to impact
}
}
}
}
?>
Related Article

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.