


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= 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
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$sql="DELETE FROM user WHERE id$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();

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

Dreamweaver CS6
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use

Atom editor mac version download
The most popular open source editor