Home > Backend Development > PHP Tutorial > In fact, there is no desire today..-MySQLi, desire..-MySQLi_PHP Tutorial

In fact, there is no desire today..-MySQLi, desire..-MySQLi_PHP Tutorial

WBOY
Release: 2016-07-12 09:03:35
Original
1348 people have browsed it

In fact, I have no desire today..-MySQLi, desire..-MySQLi

 hi

I had a refreshing swim at noon, but after finishing reading One Punch Man in the afternoon, I no longer had the desire to learn. . . Force yourself to update something and read a book later.

1. MySQLi

2. MySQLi OOP-based programming

2.1 Using parsing

--Basic

MySQLi is an extended class library, essentially a class (?).

The general process is the same as MySQL: connection, library selection, character set setting, SQL statement execution, closing connection .

--Link library example

/*
* Connect and select database
*/
$mysqli=new mysqli('localhost', 'root' , '');
print_r($mysqli);echo "
";

echo $mysqli->select_db('test');echo "
";

$mysqli2=new mysqli();
print_r($mysqli2->connect('localhost', 'root', ''));echo "
";

print_r($mysqli3=new mysqli('localhost', 'root', '','test'));echo "
";

Three different methods, the methods here are all done using the class attributes of mysqli; of course, you can also use mysqli commands to link;

$con=mysqli_connect(HOST,USERNAME,PASSWORD)

There is some information in the results

mysqli Object
  (
  [affected_rows] => 0
  [client_info] => mysqlnd 5.0.11-dev - 20120503 - $Id: bf9ad53b11c9a57efdb1057292d73b928b8c5c77 $
  [client_version] => 50011
  [connect_errno] => 0
  [connect_error] =>
  [errno] => 0
  [error] =>
  [error_list] => Array
  (
  )
   
  [field_count] => 0
  [host_info] => localhost via TCP/IP
  [info] =>
  [insert_id] => 0
  [server_info] => 5.6.17
  [server_version] => 50617
  [stat] => Uptime: 968 Threads: 1 Questions: 24 Slow queries: 0 Opens: 70 Flush tables: 1 Open tables: 63 Queries per second avg: 0.024
  [sqlstate] => 00000
  [protocol_version] => 10
  [thread_id] => 11
  [warning_count] => 0
  )

These attributes can be obtained through the attributes of the object, such as

echo $mysqli->client_info;echo "
";

Or it can be obtained through corresponding methods. You can see these things in the manual.

header('content-type:text/html;charset=utf-8');
//1. Establish a connection to MySQL data
// $mysqli= new mysqli('localhost','root','root');
// //print_r($mysqli);
// //2. Open the specified database
// $mysqli- >select_db('test');
// $mysqli=new mysqli();
// $mysqli->connect('127.0.0.1','root','root');
// print_r($mysqli);

//Open the specified database while establishing a connection
$mysqli=@new mysqli('localhost','root','root','test');
//print_r($mysqli);
//$mysqli->connect_errno: Get the error number generated by the connection
//$mysqli->connect_error: Get the error message generated by the connection
if($mysqli->connect_errno){
die('Connect Error:'.$mysqli->connect_error);
}
print_r($mysqli);
echo '


';
echo 'Client information:'.$mysqli->client_info.'
';
echo $mysqli->get_client_info().'
';
echo 'Client version:'.$mysqli->client_version.'
';
echo '
';
echo 'Server-side information:'.$ mysqli->server_info.'
';
echo $mysqli->get_server_info();
echo '
';
echo 'Server version:' .$mysqli->server_version.'
';

echo '


';

--Character set example

//1. Establish a connection to MySQL
$mysqli=@new mysqli('localhost','root','root','test');
if ($mysqli->connect_errno){
die('Connect Error:'.$mysqli->connect_error);
}
//2. Set the default client encoding utf8
$mysqli->set_charset('utf8');

///3. Execute SQL query
$sql=<< CREATE TABLE IF NOT EXISTS mysqli(
id TINYINT UNSIGNED AUTO_INCREMENT KEY,
username VARCHAR(20) NOT NULL
);
EOF;
$res=$mysqli->query($sql);
var_dump($res);

/*
SELECT/DESC/DESCRIBE/SHOW/EXPLAIN returns the mysqli_result object if the execution is successful, and false if the execution fails
For the execution of other SQL statements, true is returned if the execution is successful, otherwise false
*/
//Close the connection
$mysqli->close();

It should be noted that the database is utf8, not utf-8;

2.2 Insert record operation

Increase.

--connect.php

Because a series of operations to connect to the database are commonly used, for this, our simple method is to encapsulate it and call

everywhere

require_once 'connect.php';

connect.php

/*
* Connection and library selection (header) files
*/
$mysqli=new mysqli('localhost', 'root ', '','test');
if($mysqli->connect_errno){
die('Connect Error:'.$mysqli->connect_error);
}else{
echo 'Client information:'.$mysqli->client_info.'
';
}
$mysqli->set_charset('utf8');

--Added

/*
* Insert data into database
*/

require_once 'connect.php';

$sql="insert mysqli(username) value('Tom')";
echo $mysqli->query($sql);

What is executed here is a single sql statement.

Or improve it a little, add a judgment, and output error information.

if($res){
echo $mysqli->insert_id;
}else{
echo 'ERROR '.$mysqli->error;
}

Or, insert multiple records

$sql="insert mysqli(username) value('Sdaf'),('Andy')";

2.3 Update Record

Updated.

$sql="update test set id=id 10";
$mysqli->query($sql);

2.4 Delete

Delete

$sql="delete from mysqli where id>=2";

--

Special note, there are three situations returned by affected_rows:

-1 There is a problem with the sql statement;

0 No affected statements;

>=0 Number of affected items.

--Summary

header('content-type:text/html;charset=utf-8');
$mysqli=new mysqli('localhost','root','root', 'test');
if($mysqli->connect_errno){
die('CONNECT ERROR:'.$mysqli->connect_error);
}
$mysqli->set_charset ('utf8');

//Execute SQL query
//Add record
//Execute a single SQL statement, only one SQL statement can be executed
// $sql="INSERT user(username,password) VALUES(' king','king');";
// $sql.="DROP TABLE user;";
$sql="INSERT user(username,password) VALUES('queen1','queen1') ,('queen2','queen2'),('queen3','queen3'),('queen4','queen4')";
$res=$mysqli->query($sql);
if($res){
//Get the value of AUTO_INCREMENT generated by the previous insert operation
echo 'Congratulations on your successful registration, you are the '.$mysqli->insert_id.' user on the website< ;br/>';
//Get the number of affected records generated by the previous operation
echo 'There are'.$mysqli->affected_rows.' records affected';
}else{
//Get the error number and error message generated by the previous operation
echo 'ERROR '.$mysqli->errno.':'.$mysqli->error;
}
echo '


';

//Add age 10 in the table
$sql="UPDATE user SET age=age 10";
$res=$mysqli->query($sql);
if($res ){
echo $mysqli->affected_rows.'records updated';
}else{
echo "ERROR ".$mysqli->errno.':'.$mysqli-> error;
}
echo '


';

//Delete the user with id<=6 in the table
$sql="DELETE FROM user WHERE id<=6";
$res=$mysqli->query($sql);
if($res){
echo $mysqli->affected_rows.'Record deleted';
}else{
echo "ERROR ".$mysqli->errno.': '.$mysqli->error;
}
//Close the connection to MySQL
$mysqli->close();

2.5 Check

It should be noted that select is used, so the result set is returned, which is print_r or var_dump that can be printed out.

So here we will talk about the selection of the returned result set.

header('content-type:text/html;charset=utf-8');
$mysqli=new mysqli('localhost','root','root', 'test');
if($mysqli->connect_errno){
die('CONNECT ERROR:'.$mysqli->connect_error);
}
$mysqli->set_charset ('utf8');
$sql="SELECT id,username,age FROM user";
$mysqli_result=$mysqli->query($sql);
/ /var_dump($mysqli_result);
if($mysqli_result && $mysqli_result->num_rows>0){
//echo $mysqli_result->num_rows;
//$rows=$ mysqli_result->fetch_all();//Get all records in the result set, the default return is two-dimensional
//Form of index index
//$rows=$mysqli_result->fetch_all( MYSQLI_NUM);
//$rows=$mysqli_result->fetch_all(MYSQLI_ASSOC);
//$rows=$mysqli_result->fetch_all(MYSQLI_BOTH);
// $row= $mysqli_result->fetch_row();//Get a record in the result set and return it as an index array
// print_r($row);
// echo '


';
// $row=$mysqli_result->fetch_assoc();//Get a record in the result set and return it as an associative array
// print_r($row);
// echo '
';
// $row=$mysqli_result->fetch_array();//Both
// print_r($row) ;

// echo '
';
// $row=$mysqli_result->fetch_array(MYSQLI_ASSOC);
// print_r( $row);

// echo '
';
// $row=$mysqli_result->fetch_object();
// print_r($row);
// echo '
';
// //Move the result set internal pointer
// $mysqli_result->data_seek(0);
// $row=$mysqli_result->fetch_assoc();
// print_r($row);

// print_r($rows);

while($row=$mysqli_result->fetch_assoc()){
//print_r($row);
//echo '


';
$rows[ ]=$row;
}
print_r($rows);

//Release the result set
$mysqli_result->free();


}else{
echo 'Query error or no record in the result set';
}
$mysqli->close();

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1079207.htmlTechArticleIn fact, I have no desire today...-MySQLi, desire...-MySQLi hi I had a refreshing swim at noon , but after watching One Punch Man in the afternoon, I no longer have the desire to learn. . . Force yourself...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template