亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

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
提交
取消