为了账号安全,请及时绑定邮箱和手机立即绑定

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

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.

原文链接:http://outofmemory.cn/mysql/mysql-prepared-statement

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

举报

0/150
提交
取消