当前位置:网站首页>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; ?>
边栏推荐
- ArrayList analysis 3: delete elements
- What is the difference between cloud server and cloud virtual machine
- link preload prefetch
- QT学习日记9——对话框
- MySQL has been stopped in the configuration interface during installation
- WEB-UI自动化测试-最全元素定位方法
- STM32H7 HAL库SPI DMA发送一直处于busy的解决办法
- Kotlin's collaboration: Context
- 解决Zabbix用snmp监控网络流量不准的问题
- TCP拥塞控制详解 | 3. 设计空间
猜你喜欢

Implementation of Tetris in C language

QT学习日记9——对话框

1146_ SiCp learning notes_ exponentiation

Golang unit test, mock test and benchmark test

微服务组件Sentinel控制台调用

The third day of writing C language by Yabo people

Internet Hospital his Management Platform source, online Inquiry, appointment Registration Smart Hospital Small program source
![[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

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

聊聊支付流程的设计与实现逻辑
随机推荐
SSL / bio pour OpenSSL Get FD
Play with fancy special effects. This AE super kit is for you
1164 Good in C
c# . Net tool ecosystem
[combinatorics] generating function (shift property)
OpenSSL的SSL/BIO_get_fd
Tensorboard quick start (pytoch uses tensorboard)
[combinatorics] recursive equation (summary of the solution process of recursive equation | homogeneous | double root | non-homogeneous | characteristic root is 1 | exponential form | the bottom is th
互联网医院HIS管理平台源码,在线问诊,预约挂号 智慧医院小程序源码
[教程]在 CoreOS 上构建你的第一个应用
The gbase 8A database does not support the DB2 function value (column_name, 0) cluster syntax
鸿蒙第三次培训
MySQL grouping query
Servlet specification Part II
互聯網醫院HIS管理平臺源碼,在線問診,預約掛號 智慧醫院小程序源碼
Automata and automatic line of non-standard design
UE4 official charging resources, with a total price of several thousand
Talk about the design and implementation logic of payment process
企业级自定义表单引擎解决方案(十一)--表单规则引擎1
vs2013已阻止安装程序,需安装IE10
