PHP mysqli Extension Library vs MySQL usage

Source: Internet
Author: User

1, in PHP using the mysqli extension library to the MySQL DQL operation

The code is as follows Copy Code


<?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

The code is as follows Copy Code

<?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

The code is as follows Copy Code


<?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

The code is as follows Copy Code

?

    class sqlhelper{
       
         private $mysqli;
       //Here first write dead, later write dead things in a file to configure
         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 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
}
}
}
}
?>

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.