A detailed description of the stored procedures in PDO

Source: Internet
Author: User
Tags dsn php language
A detailed description of the stored procedures in PDO

The stored procedures in PDO allow data to be manipulated closer to the data, thereby reducing the use of bandwidth, which makes the data independent of the scripting logic, allowing multiple systems in different languages to access the data in the same way, saving valuable time spent on coding and debugging, and using predefined scenarios to perform operations, Improve query speed, and can prevent the direct interaction with the data, thereby playing the role of protecting data!

In the previous article, "How to deal with PDO," we introduced the transaction processing of PDO, so this article introduces you to the stored procedures in PDO!

Let's start by explaining how to call a stored procedure in PDO, where you first create a stored procedure with the following SQL statement:

drop procedure if exists pro_reg;delimiter//create procedure Pro_reg (in NC varchar), in pwd varchar (+), in email varcha R (N), in address varchar (a)) Begininsert into Tb_reg (name,pwd,email,address) values (nc,pwd,email,address); end;//

The "Drop" statement removes the stored procedure pro_reg that already exists in the MySQL server.

The function of "delimiter//" is to think of the statement terminator as "//".

In NC varchar ... in address varchar (50) "indicates the parameters to be passed in the stored procedure.

"Begin......end" represents a block of statements in a stored procedure that acts like "{...}" in the PHP language.

After the stored procedure is created successfully, the following calls the stored procedure to implement the user registration information add operation, the following steps.

Create a index.php file. First, create a form form and submit the user information to this page via the Post method. Then, write PHP scripts on this page, connect the MySQL database via PDO, and set the database encoding format to UTF-8 to get the user registration information submitted in the form. The stored procedure Pro_reg is then called through the Call statement to add the user registration information to the data table. Finally, by try...catch ... The statement block returns an error message. The key code is as follows:

<form name= "Form1" action= "4.php" method= "POST" > User nickname: <input type= "text" Name= "NC" ><br> password:<  Input type= "password" name= "password" ><br> email: <input type= "text" name= "email" ><br> <input Type= "Submit" name= "submit" value= "registration" > <input type= "Submit" name= "submit" value= "rewrite" &GT;&LT;/FORM&GT;&LT;? Phpif ($_post["Submit"]) {header ("content-type:text/html;    Charset=utf-8 ");                                  Set the encoding format of the page $dbms = "MySQL";                                The type of database $dbname = "PHP_CN";                                   The database name used is $user = "root";                                    Database user name used $pwd = "root";                              The database password used $host = "localhost"; Host name used $DSN = "$dbms: host= $host;d bname= $dbName"; try {$pdo = new PDO ($DSN, $user, $pwd);//Initialize a PDO object to create a database connection object $pd    o $pdo->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 query statement if ($res->execute ()) {echo "Add database Success";    } else {echo "Failed to add Database";    }}catch (pdoexception $e) {echo "PDO Exception caught";    Echo ' Error with the database:<br> '; Echo ' SQL Query; '.    $query;    Echo ' <pre> '; echo "Error:". $e-GetMessage (). "    <br> "; echo "Code:". $e->getcode (). "    <br> "; echo "File:". $e->getfile (). "    <br> "; echo "line:". $e->getline (). "    <br> "; echo "Trace:". $e->gettraceasstring (). "    <br> "; echo "</pre>";}}

The results are as follows:

The main is to create a stored procedure, and then call this stored procedure, the small partners can be local to try it! ~

Summarize:

About the PDO database abstraction layer here is all over, we focus on the database abstraction layer--pdo, from the PDO overview, features and installation began to explain, to the PDO instance application, including: How to connect different databases, how to execute SQL statements, how to get the result set and perform error handling, To the advanced application of PDO: Transactions and stored procedures, and each part has a corresponding instance, through this chapter of learning, I believe that the small partners can master the application of PDO technology. Then our next topic is gone!

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.