prepared statement mysql_MySQL数据库之MySQL Prepared Statement

本文主要向大家介绍了MySQL数据库之MySQL Prepared Statement ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助。

Summary: in this tutorial, you will learn how to use MySQL prepared statement to make your queries execute faster and more secure.

Introduction to MySQL Prepared Statement

Prior MySQL version 4.1, the query is sent to the MySQL server in the textual format. In turn, MySQL returns the data to the client using textual protocol. MySQL has to parse the query fully and coverts the result set into a string before returning it to the client.

The textual protocol has serious performance implication. To resolve this problem, MySQL added a new feature called prepared statement since version 4.1.

The prepared statement takes advantage of client/server binary protocol. It passes query that contains placeholders (?) to the MySQL server as the following example:

SELECT *  FROM products  WHERE productCode = ?

When MySQL executes this query with different productcode values, it does not have to parse the query fully. As a result, this helps MySQL execute the query faster, especially when MySQL executes the query multiple times. Because the prepared statement uses placeholders (?), this helps avoid many variants of SQL injection hence make your application more secure.

MySQL prepared statement usage

In order to use MySQL prepared statement, you need to use other three MySQL statements as follows:

PREPARE – Prepares statement for execution.

EXECUTE – Executes a prepared statement preparing by a PREPARE statement.

DEALLOCATE PREPARE – Releases a prepared statement.

The following diagram illustrates how to use the prepared statement:

MySQL prepared statement example

Let’s take a look at an example of using the MySQL prepared statement.

PREPARE stmt1 FROM 'SELECT productCode, productName                     FROM products                     WHERE productCode = ?'; SET @pc = 'S10_1678'; EXECUTE stmt1 USING @pc; DEALLOCATE PREPARE stmt1;

First we used the PREPARE statement to prepare a statement for execution. We used the SELECT statement to query product data from the  products table based on a specified product code. We used question mark (?) as a placeholder for the product code.

Next, we declared a product code variable  @pc and set it values to S10_1678.

Then, we used the EXECUTE statement to execute the prepared statement with product code variable @pc.

Finally, we used the  DEALLOCATE PREPARE to release the prepared statement.

In this tutorial, we have shown you how to use MySQL prepared statement to execute a query with placeholders to improve the speed of the query and make your query more secure.

本文由职坐标整理并发布,希望对同学们学习MySQL有所帮助,更多内容请关注职坐标数据库MySQL数据库频道!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值