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);