实例解析MySQL中的存储过程及存储过程的调用方法_MySQL
mysql在5.1之后增加了存储过程的功能, 存储过程运行在mysql内部,语句都已经编译好了,速度比sql更快. 存储过程与mysql相当于shell和linux系统。如果你是程序员的话,那我告诉你存储过程实际上是一个方法,你只要调用这个方法,并且输入它设置好的参数就可以获取或者执行你想要的操作了. 看了如下存储过程实例,你会发现mysql存储过程和shell很像.
下面存储过程内容为:调用存储过程,并且传入用户名,密码参数。存储过程会将这她们存储到process_test表里面.看实例
一,创建数据库
mysql>create database db_proc;
二,创建表
mysql>CREATE TABLE `proc_test` ( `id` tinyint(4) NOT NULL AUTO_INCREMENT, #ID,自动增长 `username` varchar(20) NOT NULL, #用户名 `password` varchar(20) NOT NULL, #密码 PRIMARY KEY (`id`) #主键 ) ENGINE=MyISAM AUTO_INCREMENT=50 DEFAULT CHARSET=utf8; #设置表引擎和字符集
三、创建存储过程
create procedure mytest(in name varchar(20),in pwd varchar(20))#定义传入的参数 begin insert into proc_test(username,password) values(name,pwd); #把传进来的参数name和pwd插入表中,别忘记分号 end; #注意这个分号别忘记了 create procedure mytest(in name varchar(20),in pwd varchar(20))#定义传入的参数 begin insert into proc_test(username,password) values(name,pwd); #把传进来的参数name和pwd插入表中,别忘记分号 end; #注意这个分号别忘记了
四、测试调用存储过程
用法:call 存储过程名称(传入的参数)
call proc_test("绝心是凉白开","www.bitsCN.com")
username为”绝心是凉白开“传入数据库中,密码”www.bitsCN.com“
五、查看数据库中有无加入的数据
select * from proc_test where username=‘绝心是凉白开';#如果有内容说明成功了
六、删除存储过程
drop procdure 存储过程名;
代码如下:
call pr_pager('job j left join enter_job ej on j.job_no=ej.job_no','j.*,ej.*','25','1','group by ej.put_away_user order by ej.put_away_user desc','where j.job_table="enter"',@p_out_rows);
<?php function dump_single_form41report($sys_report_id) { $this->dbConn->setFetchMode(DB_FETCHMODE_ASSOC); //SET @a=1;CALL dbpi_report.simpleproc(@a);SELECT @a; $sql = "CALL dbpi_temp.dumpSingleReportForm41($sys_report_id);"; $result = $this->dbConn->query($sql); if (mysql_error()) { die (mysql_error().'<b>:</b> dump_single_form41report(...)['.__LINE__.'];<br>'.$sql); } return $result; } function initQueuePool($sys_report_id, $username){ $this->dbConn->setFetchMode(DB_FETCHMODE_ASSOC); $this->checkPreviousThread($sys_report_id, $username); $temptablename = "_".$username."_".$sys_report_id; $sql = "SET @a=".$sys_report_id.";"; $this->dbConn->query($sql); $sql = "SET @b='".DB_REPORT.".".$temptablename."';"; $this->dbConn->query($sql); $sql = "SET @c='".DB_PREPRODUCT."';"; $this->dbConn->query($sql); $sql = "CALL ".DB_REPORT.".fm41_simpleproc(@a,@b,@c);"; $this->dbConn->query($sql); }
普通的查询,只返回一个结果集,而存储过程却返回至少两个结果集,其中一个就是存储过程的执行状态。我们必须要清除了这个执行状态以后,才可能再次调用另外一个存储过程 。
<?php $rs=mysql_query("call pr_pager('change_monitor','*',10,1,'','',@p_out_rows)"); while($rows=mysql_fetch_array($rs)){ echo $rows[Schedule]; } $query=mysql_query("select @p_out_rows"); $v=mysql_fetch_array($query); can't return a result set in the given context
需要php调用存储过程,返回一个结果集,发现很困难,找了半天,终于在老外的论坛上找到解决方案,这里本地化一下。
关键就是两点:
1.
define('CLIENT_MULTI_RESULTS', 131072);
2.
$link = mysql_connect("127.0.0.1", "root", "",1,CLIENT_MULTI_RESULTS) or die("Could not connect: ".mysql_error());

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





You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.
