


In fact, there is no desire today..-MySQLi, desire..-MySQLi_PHP Tutorial
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=<<
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
everywhererequire_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();

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to set up keyboard startup on Gigabyte's motherboard. First, if it needs to support keyboard startup, it must be a PS2 keyboard! ! The setting steps are as follows: Step 1: Press Del or F2 to enter the BIOS after booting, and go to the Advanced (Advanced) mode of the BIOS. Ordinary motherboards enter the EZ (Easy) mode of the motherboard by default. You need to press F7 to switch to the Advanced mode. ROG series motherboards enter the BIOS by default. Advanced mode (we use Simplified Chinese to demonstrate) Step 2: Select to - [Advanced] - [Advanced Power Management (APM)] Step 3: Find the option [Wake up by PS2 keyboard] Step 4: This option The default is Disabled. After pulling down, you can see three different setting options, namely press [space bar] to turn on the computer, press group

1. Processor When choosing a computer configuration, the processor is one of the most important components. For playing games like CS, the performance of the processor directly affects the smoothness and response speed of the game. It is recommended to choose Intel Core i5 or i7 series processors because they have powerful multi-core processing capabilities and high frequencies, and can easily cope with the high requirements of CS. 2. Graphics card Graphics card is one of the important factors in game performance. For shooting games such as CS, the performance of the graphics card directly affects the clarity and smoothness of the game screen. It is recommended to choose NVIDIA GeForce GTX series or AMD Radeon RX series graphics cards. They have excellent graphics processing capabilities and high frame rate output, and can provide a better gaming experience. 3. Memory power

SPDIFOUT connection line sequence on the motherboard. Recently, I encountered a problem regarding the wiring sequence of the wires. I checked online. Some information says that 1, 2, and 4 correspond to out, +5V, and ground; while other information says that 1, 2, and 4 correspond to out, ground, and +5V. The best way is to check your motherboard manual. If you can't find the manual, you can use a multimeter to measure it. Find the ground first, then you can determine the order of the rest of the wiring. How to connect motherboard VDG wiring When connecting the VDG wiring of the motherboard, you need to plug one end of the VGA cable into the VGA interface of the monitor and the other end into the VGA interface of the computer's graphics card. Please be careful not to plug it into the motherboard's VGA port. Once connected, you can

Glodon Software is a software company focusing on the field of building informatization. Its products are widely used in all aspects of architectural design, construction, and operation. Due to the complex functions and large data volume of Glodon software, it requires high computer configuration. This article will elaborate on the computer configuration recommendations of Glodon Software from many aspects to help readers choose a suitable computer configuration processor. Glodon Software requires a large amount of data calculation and processing when performing architectural design, simulation and other operations. Therefore, the requirements for the processor are higher. It is recommended to choose a multi-core, high-frequency processor, such as Intel i7 series or AMD Ryzen series. These processors have strong computing power and multi-thread processing capabilities, and can better meet the needs of Glodon software. Memory Memory is affecting computing

Which version of the graphics card driver is best to use? 1. There is no absolute best version. It is most important to choose the version that suits your computer; 2. Because the applicability and stability of the graphics card driver version are related to the computer hardware environment and system configuration; 3. You can check the detailed information of the computer and graphics card on the official website, select the appropriate driver version based on the information, or refer to the reviews of other users. It is recommended to back up the system before installing the driver to avoid unexpected situations. Graphics card driver version 472.19 series is an excellent choice. Currently, the driver compatibility of version 472 is the best. Installing version 472 of the driver can also maximize the performance of the graphics card. The NVIDIA graphics card driver Win7 installation version, numbered 2, 472.19, is a product with remarkable quality.

I am planning to go backpacking in Tibet. ① How many liters of bag should I carry? Please tell me what you think is the best configuration. I am 170 and have good physical strength. The first time I went hiking, the amount was 60 liters or more. The amount of hiking was less than 60 liters. The entire journey was by car. You don’t need a backpack, a suitcase is more convenient. If you really need to carry something with you, a 25-40 liter bag is more than enough. Necessary supplies for Tibet travel: sunglasses, sun hat, sunscreen, skin cream, lip balm, long-sleeved top, Sweater; for special travel or travel to Ali, northern Tibet, and Sichuan-Tibet line, it is recommended to bring: sleeping bag (cold protection), sheets (dirty protection), down jacket, travel shoes or hiking shoes, slippers, toothbrush, toothpaste, towel, rolling paper , paper underwear, disinfectant wipes, flashlight, waterproof matches, knives, rope. Can a computer be carried in the front bag? Can a computer be carried in the front bag? Some backpacks have it.

Please recommend which 1155-pin CPU is the best. The current 1155-pin CPU with the highest performance is Intel Corei7-3770K. It has 4 cores and 8 threads, a base frequency of 3.5GHz, and supports TurboBoost2.0 technology, which can reach up to 3.9GHz. In addition, it is equipped with 8MB of level 3 cache and is an excellent processor with the LGA1155 pin, the most powerful CPU Intel Core i73770K. The LGA1155 interface is the interface type used by second and third generation Core processors. The best performing one is Intel Core i73770K. The parameters of this processor are as follows: 1. Applicable type: desktop; 2. CPU series: Core i7; 3. CPU

DeepSeek released a technical article on Zhihu, introducing its DeepSeek-V3/R1 inference system in detail, and disclosed key financial data for the first time, which attracted industry attention. The article shows that the system's daily cost profit margin is as high as 545%, setting a new high in global AI big model profit. DeepSeek's low-cost strategy gives it an advantage in market competition. The cost of its model training is only 1%-5% of similar products, and the cost of V3 model training is only US$5.576 million, far lower than that of its competitors. Meanwhile, R1's API pricing is only 1/7 to 1/2 of OpenAIo3-mini. These data prove the commercial feasibility of the DeepSeek technology route and also establish the efficient profitability of AI models.
