php存储过程调用实例代码
复制代码 代码如下:
//比如要调用的存储过程为gxtj(a,b)
$db=new mysqli("localhost","ssss","aaaaa","bbbb");
mysqli_query($db,"SET NAMES utf8");
$result=$db->query("call gxtj($year,$jd)"); // gxtj是mysql的存储过程名称 [color=gray][/color]
while( $row = $result->fetch_array(MYSQLI_ASSOC)) //完成从返回结果集中取出一行
{
while ($key=key($row)){ //依次取得字段名
$value=current($row); //依次取得字段值
}
}
实例一:无参的存储过程
复制代码 代码如下:
$conn = mysql_connect('localhost','root','root') or die ("数据连接错误!!!");
mysql_select_db('test',$conn);
$sql = "
create procedure myproce()
begin
INSERT INTO user (id, username, sex) VALUES (NULL, 's', '0');
end;
";
mysql_query($sql);//创建一个myproce的存储过程
$sql = "call test.myproce();";
mysql_query($sql);//调用myproce的存储过程,则数据库中将增加一条新记录。
实例二:传入参数的存储过程
复制代码 代码如下:
$sql = "
create procedure myproce2(in score int)
begin
if score >= 60 then
select 'pass';
else
select 'no';
end if;
end;
";
mysql_query($sql);//创建一个myproce2的存储过程
$sql = "call test.myproce2(70);";
mysql_query($sql);//调用myproce2的存储过程,看不到效果,可以在cmd下看到结果。
实例三:传出参数的存储过程
复制代码 代码如下:
$sql = "
create procedure myproce3(out score int)
begin
set score=100;
end;
";
mysql_query($sql);//创建一个myproce3的存储过程
$sql = "call test.myproce3(@score);";
mysql_query($sql);//调用myproce3的存储过程
$result = mysql_query('select @score;');
$array = mysql_fetch_array($result);
echo '
';print_r($array);<br> <p>实例四:传出参数的inout存储过程<br></p><p class="codetitle"><span style="CURSOR: pointer" onclick="doCopy('code35680')"><u>复制代码</u></span> 代码如下:</p><p class="codebody" id="code35680"><br>$sql = "<br>create procedure myproce4(inout sexflag int)<br>begin<br>SELECT * FROM user WHERE sex = sexflag;<br>end; <br>";<br>mysql_query($sql);//创建一个myproce4的存储过程<br>$sql = "set @sexflag = 1";<br>mysql_query($sql);//设置性别参数为1<br>$sql = "call test.myproce4(@sexflag);";<br>mysql_query($sql);//调用myproce4的存储过程,在cmd下面看效果<br></p> <p>实例五:使用变量的存储过程 <br></p><p class="codetitle"><span style="CURSOR: pointer" onclick="doCopy('code17926')"><u>复制代码</u></span> 代码如下:</p><p class="codebody" id="code17926"><br>$sql = "<br>create procedure myproce5(in a int,in b int)<br>begin<br>declare s int default 0;<br>set s=a+b;<br>select s;<br>end; <br>";<br>mysql_query($sql);//创建一个myproce5的存储过程<br>$sql = "call test.myproce5(4,6);";<br>mysql_query($sql);//调用myproce5的存储过程,在cmd下面看效果<br></p> <p>实例六:case语法</p> <p></p><p class="codetitle"><span style="CURSOR: pointer" onclick="doCopy('code44629')"><u>复制代码</u></span> 代码如下:</p><p class="codebody" id="code44629"><br>$sql = "<br>create procedure myproce6(in score int)<br>begin<br>case score<br>when 60 then select '及格';<br>when 80 then select '及良好';<br>when 100 then select '优秀';<br>else select '未知分数';<br>end case;<br>end; <br>";<br>mysql_query($sql);//创建一个myproce6的存储过程<br>$sql = "call test.myproce6(100);";<br>mysql_query($sql);//调用myproce6的存储过程,在cmd下面看效果<br></p> <p>实例七:循环语句</p> <p></p><p class="codetitle"><span style="CURSOR: pointer" onclick="doCopy('code64519')"><u>复制代码</u></span> 代码如下:</p><p class="codebody" id="code64519"><br>$sql = "<br>create procedure myproce7()<br>begin<br>declare i int default 0;<br>declare j int default 0;<br>while iset j=j+i;<br>set i=i+1;<br>end while;<br>select j;<br>end; <br>";<br>mysql_query($sql);//创建一个myproce7的存储过程<br>$sql = "call test.myproce7();";<br>mysql_query($sql);//调用myproce7的存储过程,在cmd下面看效果<br></p> <p>实例八:repeat语句</p> <p></p><p class="codetitle"><span style="CURSOR: pointer" onclick="doCopy('code31407')"><u>复制代码</u></span> 代码如下:</p><p class="codebody" id="code31407"><br>$sql = " <br>create procedure myproce8()<br>begin<br>declare i int default 0;<br>declare j int default 0;<br>repeat<br>set j=j+i;<br>set i=i+1;<br>until j>=10<br>end repeat;<br>select j;<br>end; <br>";<br>mysql_query($sql);//创建一个myproce8的存储过程<br>$sql = "call test.myproce8();";<br>mysql_query($sql);//调用myproce8的存储过程,在cmd下面看效果<br></p> <p>实例九:loop语句</p> <p></p><p class="codetitle"><span style="CURSOR: pointer" onclick="doCopy('code30744')"><u>复制代码</u></span> 代码如下:</p><p class="codebody" id="code30744"><br>$sql = "<br>create procedure myproce9()<br>begin<br>declare i int default 0;<br>declare s int default 0;</p> <p>loop_label:loop<br>set s=s+i;<br>set i=i+1;<br>if i>=5 then<br>leave loop_label;<br>end if;<br>end loop;<br>select s;<br>end; <br>";<br>mysql_query($sql);//创建一个myproce9的存储过程<br>$sql = "call test.myproce9();";<br>mysql_query($sql);//调用myproce9的存储过程,在cmd下面看效果<br></p> <p>实例十:删除存储过程</p> <p>mysql_query("drop procedure if exists myproce");//删除test的存储过程<br>实例十:存储过程中的游标<br>总结中。</p>

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 Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.
