Detailed introduction to stored procedures in PDO
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:
<form name="form1" action="4.php" method="post"> 用户昵称:<input type="text" name="nc"><br> 密 码:<input type="password" name="password"><br> 邮 箱:<input type="text" name="email"><br> <input type="submit" name="Submit" value="注册"> <input type="submit" name="Submit" value="重写"> </form> <?php if($_POST["Submit"]){ header("Content-Type:text/html; charset=utf-8"); //设置页面的编码格式 $dbms = "mysql"; // 数据库的类型 $dbName ="php_cn"; //使用的数据库名称 $user = "root"; //使用的数据库用户名 $pwd = "root"; //使用的数据库密码 $host = "localhost"; //使用的主机名称 $dsn = "$dbms:host=$host;dbname=$dbName"; try { $pdo = new PDO($dsn, $user, $pwd);//初始化一个PDO对象,就是创建了数据库连接对象$pdo $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);//准备查询语句 if ($res->execute()) { echo "添加数据库成功"; } else { echo "添加数据库失败"; } }catch (PDOException $e){ echo "PDO Exception Caught"; echo 'Error with the database:<br>'; echo 'SQL Query;'.$query; echo '<pre class="brush:php;toolbar:false">'; 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 "
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP and PDO: How to perform batch inserts and updates Introduction: When using PHP to write database-related applications, you often encounter situations where you need to batch insert and update data. The traditional approach is to use loops to perform multiple database operations, but this method is inefficient. PHP's PDO (PHPDataObject) provides a more efficient way to perform batch insert and update operations. This article will introduce how to use PDO to implement batch insert and update operations. 1. Introduction to PDO: PDO is PH

Title: Steps and Precautions for Implementing Batch Updates by Oracle Stored Procedures In Oracle database, stored procedures are a set of SQL statements designed to improve database performance, reuse code, and enhance security. Stored procedures can be used to update data in batches. This article will introduce how to use Oracle stored procedures to implement batch updates and provide specific code examples. Step 1: Create a stored procedure First, we need to create a stored procedure to implement batch update operations. The following is how to create a stored procedure

MySQL methods for deleting stored procedures include using the DROP PROCEDURE statement, using MySQL Workbench, and using command line tools. Detailed introduction: 1. Use the DROP PROCEDURE statement. The steps are to first open the MySQL client or use any tool that supports MySQL, then connect to your MySQL database, and finally execute the following SQL statement to delete the stored procedure; 2. Use MySQL Workbench to delete Stored procedures and so on.

PHP and PDO: How to query and display data in pages When developing web applications, querying and displaying data in pages is a very common requirement. Through paging, we can display a certain amount of data at a time, improving page loading speed and user experience. In PHP, the functions of paging query and display of data can be easily realized using the PHP Data Object (PDO) library. This article will introduce how to use PDO in PHP to query and display data by page, and provide corresponding code examples. 1. Create database and data tables

PDOPDO is an object-oriented database access abstraction layer that provides a unified interface for PHP, allowing you to use the same code to interact with different databases (such as Mysql, postgresql, oracle). PDO hides the complexity of underlying database connections and simplifies database operations. Advantages and Disadvantages Advantages: Unified interface, supports multiple databases, simplifies database operations, reduces development difficulty, provides prepared statements, improves security, supports transaction processing Disadvantages: performance may be slightly lower than native extensions, relies on external libraries, may increase overhead, demo code uses PDO Connect to mysql database: $db=newPDO("mysql:host=localhost;dbnam

PHP and PDO: How to perform a full-text search in a database In modern web applications, the database is a very important component. Full-text search is a very useful feature when we need to search for specific information from large amounts of data. PHP and PDO (PHPDataObjects) provide a simple yet powerful way to perform full-text searches in databases. This article will introduce how to use PHP and PDO to implement full-text search, and provide some sample code to demonstrate the process. first

PHP and PDO: How to handle JSON data in databases In modern web development, processing and storing large amounts of data is a very important task. With the popularity of mobile applications and cloud computing, more and more data are stored in databases in JSON (JavaScript Object Notation) format. As a commonly used server-side language, PHP's PDO (PHPDataObject) extension provides a convenient way to process and operate databases. Book

Stored procedures in Oracle database are a specific type of stored procedures used to execute a series of SQL statements and data operations in the database. In actual database development work, sometimes we need to determine whether a certain table exists in the database, so that we can do some judgment and logical processing in the storage process. Below we will introduce how to implement the method of determining whether a table exists in Oracle database, and provide specific code examples. First, we can use the system table user_tables or all_t
