phpmysql存储过程实例
直接上代码:
mysql_connect("localhost","user","pwd");
mysql_select_db('testdata')ordie(mysql_error());
$sql="createproceretb_neaten(inrecint,inpavarchar(15),inqydecimal(10,2),inarvarchar(6))
begin
updatetest1setqty=qty-qywhererecordnum=rec;
insertintotest2setbname=pa,area=ar,qty=qy,date=date_format(now(),'%Y%m%d'),time=date_format(now(),'%Y%m%d');
end;";
mysql_query($sql)ordie(mysql_error());
若是存储过程里含有捕获select结果的语句时,需在mysql_connect时调整参数
mysql_connect("localhost","user","password",1,131072)
执行时,直接运行
mysql_query(tb_neaten(va1,va2,va3,va4));
⑵ thinkphp调用mysql存储过程 ,求助,具体点好吗,格式猜不出来…
thinkPHP的调用
$model = M("");
$data = $model -> query("CALL abcas(1,'[email protected]')");//调用存储过程
mp($data);//输出存储过程的返回值
存储过程部分
BEGIN -- 存储过程开始
START TRANSACTION; -- 开始事务
#Routine body goes here...
SET @x = 1; -- 定义变量,通过这个变量判断知道到的地方,事务成功@x返回大于0,否则返回0
update lzh_members set user_email = em where id = tid;
if row_count() > 0 then -- 判断语句是否执行成功
update lzh_members set user_type = 0 where id = tid;
if row_count() > 0 then
update lzh_members set user_type = 2 where id = tid;
if row_count() > 0 then
SET @x = 5;
select @x;
commit; -- 事务提交
ELSE
SET @x = 0;
select @x;
rollback; -- 事务回滚
end if;
ELSE
SET @x = 0;
select @x;
rollback; -- 事务回滚
end if;
ELSE
SET @x = 0;
select @x;
rollback; -- 事务回滚
end IF;
END --存储过程结束
⑶ 请问在mysql中怎么存储图片呢
我一般是保存图片地址,查询数据库获得图片在硬盘的位置,通过其他方式显示图片
相对路径就不太清楚了,您可以试试设置环境变量,或者%HOME%之类的代码
⑷ mysql存储过程的基本用法有哪些
mysql存储过程的基本用法有哪些
在外部程序访问数据库时(例如 PHP),要组织很多 SQL 语句。
特别是业务逻辑复杂的时候,一大堆的 SQL 和条件夹杂在 PHP 代码中,让人不寒而栗。现在有了 MySQL 存储过程,业务逻辑可以封装存储过程中,这样不仅容易维护,而且执行效率也高。
一、MySQL 创建存储过程
"pr_add" 是个简单的 MySQL 存储过程,这个MySQL 存储过程有两个 int 类型的输入参数 "a"、"b",返回这两个参数的和。
复制代码 代码如下:
drop procere if exists pr_add;
计算两个数之和
复制代码 代码如下:
create procere pr_add
(
a int,
b int
)
begin
declare c int;
if a is null then
set a = 0;
end if;
if b is null then
set b = 0;
end if;
set c = a + b;
select c as sum;
/*
return c;
不能在 MySQL 存储过程中使用。return 只能出现在函数中。
*/
end;
二、调用 MySQL 存储过程
复制代码 代码如下:
call pr_add(10, 20);
执行 MySQL 存储过程,存储过程参数为 MySQL 用户变量。
复制代码 代码如下:
set @a = 10;
set @b = 20;
call pr_add(@a, @b);
三、MySQL 存储过程特点
创建 MySQL 存储过程的简单语法为:
复制代码 代码如下:
create procere 存储过程名字()
(
[in|out|inout] 参数 datatype
)
begin
MySQL 语句;
end;
MySQL 存储过程参数如果不显式指定"in"、"out"、"inout",则默认为"in"。习惯上,对于是"in" 的参数,我们都不会显式指定。
1. MySQL 存储过程名字后面的"()"是必须的,即使没有一个参数,也需要"()"
2. MySQL 存储过程参数,不能在参数名称前加"@",如:"@a int"。下面的创建存储过程语法在 MySQL 中是错误的(在 SQL Server 中是正确的)。 MySQL 存储过程中的变量,不需要在变量名字前加"@",虽然 MySQL 客户端用户变量要加个"@"。
复制代码 代码如下:
create procere pr_add
(
@a int, -- 错误
b int -- 正确
)
3. MySQL 存储过程的参数不能指定默认值。
4. MySQL 存储过程不需要在 procere body 前面加 "as"。而 SQL Server 存储过程必须加 "as" 关键字。
复制代码 代码如下:
create procere pr_add
(
a int,
b int
)
as -- 错误,MySQL 不需要 "as"
begin
mysql statement ...;
end;
5. 如果 MySQL 存储过程中包含多条 MySQL 语句,则需要 begin end 关键字。
复制代码 代码如下:
create procere pr_add
(
a int,
b int
)
begin
mysql statement 1 ...;
mysql statement 2 ...;
end;
6. MySQL 存储过程中的每条语句的末尾,都要加上分号 ";"
复制代码 代码如下:
...
declare c int;
if a is null then
set a = 0;
end if;
...
end;
7. MySQL 存储过程中的注释。
复制代码 代码如下:
/*
这是个
多行 MySQL 注释。
*/
declare c int; -- 这是单行 MySQL 注释 (注意 -- 后至少要有一个空格)
if a is null then # 这也是个单行 MySQL 注释
set a = 0;
end if;
⑸ php调用mysql存储过程(急,在线等)
关键就是两点
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());
下面就可以正常使用了,以下是例子程序。
<?php
define('CLIENT_MULTI_RESULTS', 131072);
$link = mysql_connect("127.0.0.1", "root", "",1,CLIENT_MULTI_RESULTS) or die("Could not connect: ".mysql_error());
mysql_select_db("vs") or die("Could not select database");
?>
<?php
$result = mysql_query("call get_news_from_class_id(2)") or die("Query failed:" .mysql_error());
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$line = '<tr><td><a target = _blank href=\''.$row["url"].'\'>'.$row["title"].'('.$row["page_time"].')'.'</a></td></t
r>';
echo $line;
printf("\n");
}
mysql_free_result($result);
?>
<?php
mysql_close($link);
?>
⑹ php调用mysql存储过程的问题
存储过程结果的获取和SELECT是一样的:
$row=mysql_fetch_array($res);