可以透過以下範例來理解,在該範例中我們使用準備好的語句創建了名為「Student」的表-
mysql> PREPARE stmt3 FROM 'CREATE TABLE Student(Id INT, Name Varchar(20))'; Query OK, 0 rows affected (0.00 sec) Statement prepared mysql> EXECUTE stmt3; Query OK, 0 rows affected (0.73 sec) mysql> DEALLOCATE PREPARE stmt3; Query OK, 0 rows affected (0.00 sec)
現在,在使用準備好的語句進行以下查詢的幫助下,我們可以在表格「Student」中插入值
-
mysql> PREPARE stmt7 FROM 'INSERT INTO Student(Id,Name) values(?,?)'; Query OK, 0 rows affected (0.00 sec) Statement prepared mysql> SET @A = 1, @B = 'Ram'; Query OK, 0 rows affected (0.00 sec) mysql> EXECUTE stmt7 using @A, @B; Query OK, 1 row affected (0.04 sec) mysql> SET @A = 2, @B = 'Shyam'; Query OK, 0 rows affected (0.00 sec) mysql> EXECUTE stmt7 using @A, @B; Query OK, 1 row affected (0.08 sec) mysql> SET @A = 3, @B = 'Mohan'; Query OK, 0 rows affected (0.00 sec) mysql> Select * from Student; +------+-------+ | Id | Name | +------+-------+ | 1 | Ram | | 2 | Shyam | | 3 | Mohan | +------+-------+ 3 rows in set (0.00 sec)
以上是如何建立表格並使用準備語句在該表中插入值?的詳細內容。更多資訊請關注PHP中文網其他相關文章!