Detailed introduction of stored procedures in PDO

Source: Internet
Author: User
In PDO, stored procedures allow operations on data closer to the data, thus reducing bandwidth usage. they enable data independence from script logic, multiple systems in different languages are allowed to access data in the same way, saving valuable time in coding and debugging. at the same time, he uses a predefined scheme to perform operations, improves the query speed and prevents the direct interaction with data, thus protecting data! Detailed introduction of stored procedures in PDO

In PDO, stored procedures allow operations on data closer to the data, thus reducing bandwidth usage. they enable data independence from script logic, multiple systems in different languages are allowed to access data in the same way, saving valuable time in coding and debugging. at the same time, he uses a predefined scheme to perform operations, improves the query speed and prevents the direct interaction with data, thus protecting data!

In the previous article about transaction processing in PDO, we introduced the transaction processing in PDO. so this article will introduce the stored procedure in PDO!

First, we will explain how to call a stored procedure in PDO. here we will first create a stored procedure. the SQL statement is as follows:

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))begininsert into tb_reg(name,pwd,email,address)values(nc,pwd,email,address);end;//

The "drop" statement deletes the existing stored procedure pro_reg on the MySQL server.

"Delimiter //" is used to convert the statement terminator into "//".

"In nc varchar (80) ...... in address varchar (50)" indicates the parameter to be passed into the stored procedure.

"Begin... end" indicates the statement block in the stored procedure. it is similar to "{...}" in PHP.

After the stored procedure is created, The following describes how to add user registration information by calling the stored procedure.

Create the index. php file. First, create a form and submit the user information to this page using the POST method. Then, write the PHP script on this page, connect to the MySQL database through PDO, and set the database encoding format to UTF-8 to get the user registration information submitted in the form. Then, use the call statement to call the stored procedure pro_reg and add the user registration information to the data table. Finally, an error message is returned through the try... catch... statement block. The key code is as follows:

 SetAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION); $ nc =$ _ POST ['NC ']; $ password = md5 ($ _ POST ['password']); $ email = $ _ POST ['email ']; $ query = "call pro_reg (' $ nc ',' $ password ',' $ email ')"; $ res = $ pdo-> prepare ($ query); // prepare the query statement if ($ res-> execute () {echo "database added successfully ";} else {echo "failed to add database" ;}} catch (PDOException $ e) {echo "PDO Exception Caught"; echo 'error with the database:
'; Echo' SQL Query; '. $ query; echo'
';    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:

It is mainly to create a stored procedure, and then call this stored procedure. you can try it locally ~!~

Summary:

The abstract layer of the PDO database is all over here. we will focus on the database abstraction layer-PDO, from the PDO overview, features and installation, to the PDO instance application, these include: how to connect to different databases, how to execute SQL statements, how to obtain result sets, and how to execute error processing, and then to advanced applications of PDO: transactions and stored procedures, in addition, each part has corresponding examples. through the study in this chapter, we believe that our friends can master the application of PDO technology. The next topic will not be scattered!

The above is a detailed description of the stored procedure in PDO. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.