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

Source: Internet
Author: User
1. DQL operation of MySQL using mysqli extension library in PHP
Copy CodeThe code is as follows:
Header ("Content-type:text/html;charset=utf-8");
mysqli working with MySQL database (object oriented approach)
1. Create a Mysqli object
$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 '
';*/
}
4. Close Resources
$res->free ();
$mysqli->close ();
?>

The following is a process-oriented
Copy CodeThe code is as follows:
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 '
';
}
Freeing memory
Mysqli_free_result ($res);
Mysqli_close ($MYSQLI);
?>

2. Use mysqli extension library to MySQL DML operation in PHP
Copy CodeThe code is as follows:

Using MYSQLI to extend the library's crud operations to 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 "Success";
}else{
echo "No Line affected";
}
}
Close Resource
$mysqli->close ();
?>

3, the encapsulation
Copy CodeThe code is as follows:
Class sqlhelper{

Private $mysqli;
Here to write dead, and later to write dead things with 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);
}
Set character sets
$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 row to affect
}
}
}
}
?>

http://www.bkjia.com/PHPjc/328020.html www.bkjia.com true http://www.bkjia.com/PHPjc/328020.html techarticle 1, in PHP using mysqli extension library to MySQL dql operation copy code code as follows:? PHP header ("Content-type:text/html;charset=utf-8"); mysqli working with MySQL database (object-oriented side ...)

  • 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.