当前位置:网站首页>PHP MySQL preprocessing statement
PHP MySQL preprocessing statement
2022-07-03 17:50:00 【Crooning ~ shallow singing】
Preprocessing statements and binding parameters
Preprocessing statements are used to execute multiple identical SQL sentence , And more efficient execution .
Preprocessing statements work as follows :
Preprocessing : establish SQL Statement template and send it to the database . The reserved value uses the parameter "?" Mark . for example :
INSERT INTO MyGuests (firstname, lastname, email) VALUES(?, ?, ?)
Database parsing , compile , Yes SQL The statement template performs query optimization , And store the results without outputting .
perform : Last , Pass the value of the application binding to the parameter ("?" Mark ), Database execution statement . An application can execute statements multiple times , If the values of the parameters are different .
Compared to direct execution SQL sentence , Preprocessing statements have two main advantages :
Preprocessing statements greatly reduces analysis time , Only one query was made ( Although the statement is executed multiple times ).
Binding parameters reduce server bandwidth , You only need to send the parameters of the query , Not the whole statement .
The preprocessing statement is for SQL Injection is very useful , Because different protocols are used after parameter values are sent , Ensure the legitimacy of the data .
MySQLi Preprocessing statement
The following examples are in MySQLi Preprocessing statements are used in , And bind the corresponding parameters :
example (MySQLi Use preprocessing statements )
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Detection connection if ($conn->connect_error) { die(" The connection fails : " . $conn->connect_error); } // Preprocessing and binding $stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES (?, ?, ?)"); $stmt->bind_param("sss", $firstname, $lastname, $email); // Set parameters and execute $firstname = "John"; $lastname = "Doe"; $email = "[email protected]"; $stmt->execute(); $firstname = "Mary"; $lastname = "Moe"; $email = "[email protected]"; $stmt->execute(); $firstname = "Julie"; $lastname = "Dooley"; $email = "[email protected]"; $stmt->execute(); echo " New record inserted successfully "; $stmt->close(); $conn->close(); ?>
Parse each line of code for the following example :
"INSERT INTO MyGuests (firstname, lastname, email) VALUES(?, ?, ?)"
stay SQL In the sentence , We used a question mark (?), Here we can replace the question mark with an integer , character string , Double precision floating point and Boolean values .
Next , Let's take a look bind_param() function :
$stmt->bind_param("sss", $firstname, $lastname, $email);
This function is bound to SQL Parameters of , And tell the value of the database parameter . "sss" The parameter column handles the data types of the remaining parameters .s Character tells the database that the parameter is a string .
There are four types of parameters :
- i - integer( integer )
- d - double( Double precision floating point )
- s - string( character string )
- b - BLOB(binary large object: Binary big object )
Each parameter needs to specify a type .
By telling the data type of database parameters , Can reduce the SQL The risk of Injection .
| Be careful : If you want to insert other data ( User input ), Validation of data is very important . |
|---|
PDO Preprocessing statements in
The following examples are given in PDO Preprocessing statements and binding parameters are used in :
example (PDO Use preprocessing statements )
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDBPDO"; try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); // Set up PDO The error mode is exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Preprocessing SQL And bind parameters $stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES (:firstname, :lastname, :email)"); $stmt->bindParam(':firstname', $firstname); $stmt->bindParam(':lastname', $lastname); $stmt->bindParam(':email', $email); // Insert row $firstname = "John"; $lastname = "Doe"; $email = "[email protected]"; $stmt->execute(); // Insert other rows $firstname = "Mary"; $lastname = "Moe"; $email = "[email protected]"; $stmt->execute(); // Insert other rows $firstname = "Julie"; $lastname = "Dooley"; $email = "[email protected]"; $stmt->execute(); echo " New record inserted successfully "; } catch(PDOException $e) { echo "Error: " . $e->getMessage(); } $conn = null; ?>
边栏推荐
- STM32实现74HC595控制
- Write a program to process a list container of string type. Find a special value in the container 9.27: and delete it if found. Rewrite the above procedure with deque container.
- 1164 Good in C
- [combinatorics] recursive equation (special solution form | special solution solving method | special solution example)
- TCP拥塞控制详解 | 3. 设计空间
- STM32 realizes 74HC595 control
- ArrayList分析3 : 删除元素
- Research Report on competitive strategy Outlook Analysis and investment strategic planning of China's smart home equipment industry, 2022-2028
- Select 3 fcpx plug-ins. Come and see if you like them
- QT learning diary 9 - dialog box
猜你喜欢

MySQL grouping query

Wechat applet for the first time
![[combinatorics] generating function (summation property)](/img/74/e6ef8ee69ed07d62df9f213c015f2c.jpg)
[combinatorics] generating function (summation property)

Interviewer: why is the value nil not equal to nil?

聊聊支付流程的設計與實現邏輯

鸿蒙第三次培训
![[set theory] order relation: summary (partial order relation | partial order set | comparable | strictly less than | covering | hasto | total order relation | quasi order relation | partial order rela](/img/df/a034032e203e7935dafaf8a71cb6c8.jpg)
[set theory] order relation: summary (partial order relation | partial order set | comparable | strictly less than | covering | hasto | total order relation | quasi order relation | partial order rela

IntelliJ 2021.3 short command line when running applications

1147_ Makefile learning_ Target files and dependent files in makefile

TCP拥塞控制详解 | 3. 设计空间
随机推荐
Implementation of Tetris in C language
Distributed task distribution framework gearman
Getting started with deops
Leetcode 538 converts binary search tree into cumulative tree -- recursive method and iterative method
How to install PHP on Ubuntu 20.04
MinGW compile boost library
Investigation on the operation prospect of the global and Chinese Anti enkephalinase market and analysis report on the investment strategy of the 14th five year plan 2022-2028
一入“远程”终不悔,几人欢喜几人愁。| 社区征文
Y is always discrete and can't understand, how to solve it? Answer: read it several times
1164 Good in C
鸿蒙第三次培训
WEB-UI自动化测试-最全元素定位方法
Cloud primordial weekly | CNCF released the 2021 cloud primordial development status report, which was released on istio 1.13
[Yu Yue education] family education SPOC class 2 reference materials of Shanghai Normal University
Hongmeng fourth training
Interviewer: why is the value nil not equal to nil?
STM32 realizes 74HC595 control
Supervisor monitors gearman tasks
Research Report on competitive strategy Outlook Analysis and investment strategic planning of China's smart home equipment industry, 2022-2028
The third day of writing C language by Yabo people
