Tutorial on using parameterized query sql_PHP in pdo
The methods bindParam() and bindValue() are very similar.
The only difference is that the former uses a PHP variable to bind parameters, while the latter uses a value.
So when using bindParam, the second parameter can only use variable names, not variable values, while bindValue can only use specific values.
$stm = $pdo->prepare("select * from users where user = :user");
$user = "jack";
//Correct
$stm->bindParam(":user",$user);
//Wrong
/ /$stm->bindParam(":user","jack");
//Correct
$stm->bindValue(":user",$user);
//Correct
$stm->bindValue(":user","jack");
In addition, in the stored procedure, bindParam can be bound to an input/output variable, as shown below:
$stm = $pdo->prepare("call func(:param1)");
$param1 = "abcd";
$stm->bindParam(":param1",$param1); //Correct
$stm->execute();
The results after the stored procedure is executed can be directly reflected in the variables.
For those large data block parameters in memory, for performance reasons, the former should be used first.
-------------------------------------------------- ---
http://zh.wikipedia.org/wiki/%E5%8F%83%E6%95%B8%E5%8C%96%E6%9F%A5%E8%A9%A2
Parameterized Query
Parameterized Query (Parameterized Query or Parameterized Statement) refers to using parameters (Parameter) to give values where values or data need to be filled in when designing to connect to the database and access data. This method It is currently regarded as the most effective defense method to prevent SQL injection attacks (SQL Injection). Some developers may think that using parameterized queries will make the program more difficult to maintain, or it will be very inconvenient to implement some functions [source request]. However, the additional development costs caused by using parameterized queries are usually They are far less than the heavy losses caused by attacks due to the discovery of SQL injection attack vulnerabilities.
In addition to security factors, parameterized queries often have performance advantages compared to SQL statements that concatenate strings. Because parameterized queries allow different data to reach the database through parameters, thereby sharing the same SQL statement. Most databases cache the bytecode generated by interpreting SQL statements to save the overhead of repeated parsing. If you adopt an SQL statement that concatenates strings, unnecessary overhead will be incurred by repeatedly interpreting a large number of SQL statements because the operation data is part of the SQL statement and not part of the parameters.
Table of Contents
* 1 Principle
* 2 Method of writing SQL instructions
o 2.1 Microsoft SQL Server
o 2.2 Microsoft Access
o 2.3 MySQL
o 2.4 PostgreSQL/SQLite
* 3 Client program writing methods
o 3.1 ADO.NET
o 3.2 PDO
o 3.3 JDBC
o 3.4 Cold Fusion
[edit] Principle
Using parameterized queries In this case, the database server will not treat the content of the parameters as part of the SQL command. Instead, it will apply the parameters after the database completes the compilation of the SQL command. Therefore, even if the parameters contain destructive commands, It will not be run by the database.
[edit] How to write SQL instructions
When writing SQL instructions, use parameters to represent the values that need to be filled in, for example:
[edit] Microsoft SQL Server
The parameter format of Microsoft SQL Server is It is formed by adding the "@" character to the parameter name. SQL Server also supports the anonymous parameter "?".
SELECT * FROM myTable WHERE myID = @myID
INSERT INTO myTable (c1, c2, c3, c4) VALUES (@c1, @c2, @c3, @c4)
[edit] Microsoft Access
Microsoft Access does not support named parameters and only supports anonymous parameters "?".
UPDATE myTable SET c1 = ?, c2 = ?, c3 = ? WHERE c4 = ?
[edit] MySQL
The parameter format of MySQL is composed of the "?" character plus the parameter name.
UPDATE myTable SET c1 = ?c1, c2 = ?c2, c3 = ?c3 WHERE c4 = ?c4
[edit] PostgreSQL/SQLite
The parameter format of PostgreSQL and SQLite is preceded by ":" The parameter name is formed. Of course, Access-like anonymous parameters are also supported.
UPDATE "myTable" SET "c1" = :c1, "c2" = :c2, "c3" = :c3 WHERE "c4" = :c4
[edit] Client program writing method
in Write code that uses parameters in the client code, for example:
[edit] ADO.NET
ADO.NET is used within ASP.NET.
SqlCommand sqlcmd = new SqlCommand("INSERT INTO myTable (c1, c2, c3, c4) VALUES (@c1, @c2, @c3, @c4)", sqlconn);
sqlcmd.Parameters.AddWithValue( "@c1", 1); // Set the value of parameter @c1.
sqlcmd.Parameters.AddWithValue("@c2", 2); // Set the value of parameter @c2.
sqlcmd.Parameters.AddWithValue("@c3", 3); // Set the value of parameter @c3.
sqlcmd.Parameters.AddWithValue("@c4", 4); // Set the value of parameter @c4.
sqlconn.Open();
sqlcmd.ExecuteNonQuery();
sqlconn.Close();
[edit] PDO
PDO is used within PHP. When using the PDO driver, the method of using parameter query is generally:
// Instantiate the data abstraction layer object
$db = new PDO('pgsql:host=127.0.0.1;port=5432;dbname=testdb');
// For SQL statements Execute prepare and get the PDOStatement object
$stmt = $db->prepare('SELECT * FROM "myTable" WHERE "id" = :id AND "is_valid" = :is_valid');
// Binding Parameter
$stmt->bindValue(':id', $id);
$stmt->bindValue(':is_valid', true);
// Query
$stmt- >execute();
//Get data
foreach($stmt as $row) {
var_dump($row);
}
[code]
For MySQL Specific drivers can also be used like this:
$db = new mysqli("localhost", "user", "pass", "database");
$stmt = $mysqli -> prepare("SELECT priv FROM testUsers WHERE username=? AND password=?");
$stmt -> bind_param("ss", $user, $pass);
$stmt -> execute();
Worth Note that although the following method can effectively prevent SQL injection (thanks to the escaping of the mysql_real_escape_string function), it is not a true parameterized query. Its essence is still a SQL statement that concatenates strings.
[code]
$query = sprintf("SELECT * FROM Users where UserName='%s' and Password='%s'",
mysql_real_escape_string($Username),
mysql_real_escape_string($ Password));
mysql_query($query);
[edit] JDBC
JDBC is used in Java.
java.sql.PreparedStatement prep = connection.prepareStatement(
"SELECT * FROM `users` WHERE USERNAME = ? AND PASSWORD = ?");
prep.setString(1, username);
prep.setString(2, password);
prep.executeQuery();
[edit] Cold Fusion
SELECT *
FROM COMMENTS
WHERE COMMENT_ID =

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

Tomato Novel is a very popular novel reading software. We often have new novels and comics to read in Tomato Novel. Every novel and comic is very interesting. Many friends also want to write novels. Earn pocket money and edit the content of the novel you want to write into text. So how do we write the novel in it? My friends don’t know, so let’s go to this site together. Let’s take some time to look at an introduction to how to write a novel. Share the Tomato novel tutorial on how to write a novel. 1. First open the Tomato free novel app on your mobile phone and click on Personal Center - Writer Center. 2. Jump to the Tomato Writer Assistant page - click on Create a new book at the end of the novel.

HQL and SQL are compared in the Hibernate framework: HQL (1. Object-oriented syntax, 2. Database-independent queries, 3. Type safety), while SQL directly operates the database (1. Database-independent standards, 2. Complex executable queries and data manipulation).

Unfortunately, people often delete certain contacts accidentally for some reasons. WeChat is a widely used social software. To help users solve this problem, this article will introduce how to retrieve deleted contacts in a simple way. 1. Understand the WeChat contact deletion mechanism. This provides us with the possibility to retrieve deleted contacts. The contact deletion mechanism in WeChat removes them from the address book, but does not delete them completely. 2. Use WeChat’s built-in “Contact Book Recovery” function. WeChat provides “Contact Book Recovery” to save time and energy. Users can quickly retrieve previously deleted contacts through this function. 3. Enter the WeChat settings page and click the lower right corner, open the WeChat application "Me" and click the settings icon in the upper right corner to enter the settings page.

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

Setting font size has become an important personalization requirement as mobile phones become an important tool in people's daily lives. In order to meet the needs of different users, this article will introduce how to improve the mobile phone use experience and adjust the font size of the mobile phone through simple operations. Why do you need to adjust the font size of your mobile phone - Adjusting the font size can make the text clearer and easier to read - Suitable for the reading needs of users of different ages - Convenient for users with poor vision to use the font size setting function of the mobile phone system - How to enter the system settings interface - In Find and enter the "Display" option in the settings interface - find the "Font Size" option and adjust it. Adjust the font size with a third-party application - download and install an application that supports font size adjustment - open the application and enter the relevant settings interface - according to the individual

Mobile games have become an integral part of people's lives with the development of technology. It has attracted the attention of many players with its cute dragon egg image and interesting hatching process, and one of the games that has attracted much attention is the mobile version of Dragon Egg. To help players better cultivate and grow their own dragons in the game, this article will introduce to you how to hatch dragon eggs in the mobile version. 1. Choose the appropriate type of dragon egg. Players need to carefully choose the type of dragon egg that they like and suit themselves, based on the different types of dragon egg attributes and abilities provided in the game. 2. Upgrade the level of the incubation machine. Players need to improve the level of the incubation machine by completing tasks and collecting props. The level of the incubation machine determines the hatching speed and hatching success rate. 3. Collect the resources required for hatching. Players need to be in the game

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension
