当前位置:网站首页>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; ?>
边栏推荐
- UE4 official charging resources, with a total price of several thousand
- 1164 Good in C
- OpenSSL的SSL/BIO_get_fd
- 分布式的任务分发框架-Gearman
- Hongmeng fourth training
- Loop through JSON object list
- Ssl/bio of OpenSSL_ get_ fd
- How to deploy applications on kubernetes cluster
- Draw some simple graphics with MFC
- ArrayList分析3 : 删除元素
猜你喜欢
Codeforces Round #803 (Div. 2) C. 3SUM Closure
面试官:值为 nil 为什么不等于 nil ?
How to purchase Google colab members in China
1147_ Makefile learning_ Target files and dependent files in makefile
Research Report on investment trends and development planning of China's thermal insulation material industry, 2022-2028
微服务组件Sentinel控制台调用
Leetcode 538 converts binary search tree into cumulative tree -- recursive method and iterative method
Tensorboard quick start (pytoch uses tensorboard)
Qt调节Win屏幕亮度和声音大小
AcWing 271. 杨老师的照相排列【多维DP】
随机推荐
Getting started with deops
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
Life perception 1
PHP returns 500 errors but no error log - PHP return 500 error but no error log
Y is always discrete and can't understand, how to solve it? Answer: read it several times
Basic grammar of interview (Part 2)
Applet setting multi account debugging
Tensorboard quick start (pytoch uses tensorboard)
Fedora 21 安装 LAMP 主机服务器
ES6类的继承
Research Report on competitive strategy Outlook Analysis and investment strategic planning of China's smart home equipment industry, 2022-2028
解决Zabbix用snmp监控网络流量不准的问题
Is AI too slow to design pictures and draw illustrations? 3 sets of practical brushes to save you
link preload prefetch
i++与++i的区别:通俗易懂的讲述他们的区别
STM32H7 HAL库SPI DMA发送一直处于busy的解决办法
VM11289 WAService. js:2 Do not have __ e handler in component:
AcWing 3438. 数制转换
SSL / bio pour OpenSSL Get FD
Leetcode 538 converts binary search tree into cumulative tree -- recursive method and iterative method