Detailed introduction to stored procedures in PDO
The stored procedures in PDO allow data to be manipulated closer to the data, thereby reducing bandwidth usage. They make data independent of script logic, allowing multiple systems using different languages to access data in the same way, saving valuable time spent on coding and debugging, while he uses predefined scenarios to perform operations and increase query speed. And it can prevent direct interaction with data, thus protecting the data!
In the previous article "Detailed Introduction to Transaction Processing in PDO", we introduced the transaction processing of PDO, so in this article we will introduce to you the stored procedures in PDO!
First, let’s explain how to call a stored procedure in PDO. Here we first create a stored procedure with the following SQL statement:
drop procedure if exists pro_reg; delimiter// create procedure pro_reg(in nc varchar(80),in pwd varchar(80), in email varchar(80),in address varchar(50)) begin insert into tb_reg(name,pwd,email,address)values(nc,pwd,email,address); end; //
The “drop” statement deletes the existing stored procedure pro_reg in the MySQL server. .
The function of "delimiter//" is to change the statement end character to "//".
"in nc varchar(80)....in address varchar(50)" indicates the parameters to be passed into the stored procedure.
"begin...end" represents the statement block in the stored procedure, and its function is similar to "{.......}" in the PHP language.
After the stored procedure is successfully created, the stored procedure is called below to add user registration information. The specific steps are as follows.
Create the index.php file. First, create a form and submit user information to this page through the POST method. Then, write a PHP script on this page, connect to the MySQL database through PDO, and set the database encoding format to UTF-8 to obtain the user registration information submitted in the form. Then, call the stored procedure pro_reg through the call statement to add the user registration information to the data table. Finally, error information is returned through the try...catch... statement block. The key code is as follows:
'; echo "Error:".$e -> getMessage()."
"; echo "Code:".$e ->getCode()."
"; echo "File:".$e ->getFile()."
"; echo "Line:".$e ->getLine()."
"; echo "Trace:".$e ->getTraceAsString()."
"; echo "
The result is as follows:
The main thing is to create a stored procedure, and then call this stored procedure. Friends can do it locally Try it yourself~! ~
Summary:
The PDO database abstraction layer is all over here. We focus on the database abstraction layer-PDO, from the overview, characteristics and The installation begins with an introduction to PDO example applications, including: how to connect to different databases, how to execute SQL statements, how to obtain result sets and perform error handling, and then to advanced applications of PDO: transactions and stored procedures, and each part is There are corresponding examples. Through studying this chapter, I believe that friends can master the application of PDO technology. Then see you in our next topic!
The above is the detailed content of Detailed introduction to stored procedures in PDO. For more information, please follow other related articles on the PHP Chinese website!