在大学就学习过存储过程,但是实际项目中从来没用到,现在有些大项目需要用到,所以就写了个超级简单存储过程来学习一下。
MySQL代码:首先创建一个存储过程(查询所有用户信息的存储过程)
creat procedure find_userinfo() select * from userinfo;
调用代码:CALL find_userinfo();
在PHP中调用方法:
$conn = mysql_connect('localhost','root','root') or die ("数据连接错误!!!");
mysql_select_db('test',$conn);
$sql = "call find_userinfo();";
mysql_query($sql);//调用find_userinfo的存储过程,则返回的结果为查询的所有用户信息。
是不是超级简单。下面就写一个稍微复杂一丢丢的存储过程。
PHP中创建存储过程并且调用:
$conn = myslq_connect('localhost','root','root') or die("数据连接错误!");
mysql_select_db('test',$conn);
$sql = "creat proceduce myproce()
begin
Insert into user(id,username,sex) value (NULL,'s','0');
end;
";
mysql_query($sql);//创建一个myproce的存储过程
$sql = "call test.myproce();";
mysql_query($sql);//调用myproce的存储过程,则数据库中将增加一条新记录。
评论 (0)