Home > Database > Mysql Tutorial > body text

How to create a table and insert values ​​into that table using prepared statements?

WBOY
Release: 2023-08-25 23:17:18
forward
1047 people have browsed it

How to create a table and insert values ​​into that table using prepared statements?

It can be understood through the following example where we have created a table named "Student" using prepared statements -

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)
Copy after login

Now , with the help of the following query using prepared statements, we can insert the values ​​

-

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)
Copy after login
in the table "Student"

The above is the detailed content of How to create a table and insert values ​​into that table using prepared statements?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!