本文目錄一覽:
php調用mysql存儲過程,如何實現。 我的代碼如下:
mysql存儲過程返回2個資源,第一個是執行信息,第二個是存儲過程返回結果。
mysql_*系列函數無法獲取超過1個資源,需使用mysqli或PDO代替。
PDO:
$stmt = $db-prepare(“CALL pro_rb_save(?,?,@return_msg);”);
$stmt-bindParam(1, $a);
$stmt-bindParam(2, $b);
$stmt-execute ();
$outputArray = $db-query(“select @return_msg”)-fetch(PDO::FETCH_ASSOC);
var_export($return_msg);
php調用oracle存儲過程與函數
對於存儲過程的源代碼,開始都需要先定義接受的參數,例如:
PROCEDURE edit_entry(
status_out OUT NUMBER,
status_msg_out OUT VARCHAR2,
id_inout IN OUT INTEGER,
title_in IN VARCHAR2,
text_out OUT CLOB,
categories_in IN list_of_numbers
);
從 PHP 中調用存儲過程 對於要從 PHP 中執行以調用過程的 SQL 語句而言,您將通常在 Oracle BEGIN …END; 塊(稱作匿名塊)中嵌入調用。例如:
?php
// etc.
//$sql = ‘BEGIN sayHello(:name, :message); END;’;
//然後,通過調用 oci_bind_by_name() 將參數綁定到 PHP 變量。 如果使用以下 DDL 語句定義了 sayHello
//:
//CREATE OR REPLACE PROCEDURE
//sayHello (name IN VARCHAR2, greeting OUT VARCHAR2)
//AS
//BEGIN
//greeting := ‘Hello ‘ || name;
//END;
//
//注意,您可以使用 SQL*Plus 命令行運行上面的語句。將該語句保存到文件 (SAYHELLO.SQL)。接下來,使用
//SQL*Plus 登錄:
// $ sqlplus username@SID
// 然後,使用 START 命令創建該過程:
// SQL START /home/username/SAYHELLO.SQL
// 以下 PHP 腳本調用該過程:
$conn = oci_connect(‘SCOTT’,’TIGER’) or die;
$sql = ‘BEGIN sayHello(:name, :message); END;’;
$stmt = oci_parse($conn,$sql);
// Bind the input parameter
oci_bind_by_name($stmt,’:name’,$name,32);
// Bind the output parameter
oci_bind_by_name($stmt,’:message’,$message,32);
// Assign a value to the input
$name = ‘Harry’;
oci_execute($stmt);
// $message is now populated with the output value
print “$message\n”;
?
調用程序包中的過程時,將使用句號來分隔程序包名稱與過程名稱。 可以使用以下語句指定 blog 程序包:
CREATE OR REPLACE PACKAGE blog AS
TYPE cursorType IS REF CURSOR RETURN blogs%ROWTYPE;
/*
Fetch the latest num_entries_in from the blogs table, populating
entries_cursor_out with the result
*/
PROCEDURE latest(
num_entries_in IN NUMBER,
entries_cursor_out OUT cursorType
);
/*
Edit a blog entry.If id_inout is NULL, results in an INSERT, otherwise
attempts to UPDATE the existing blog entry. status_out will have the value
1 on success, otherwise a negative number on failure with status_msg_out
containing a description
categories_in is a collection where list_of_numbers is described by
TYPE list_of_numbers AS VARRAY(50) OF NUMBER;
*/
PROCEDURE edit_entry(
status_out OUT NUMBER,
status_msg_out OUT VARCHAR2,
id_inout IN OUT INTEGER,
title_in IN VARCHAR2,
text_out OUT CLOB,
categories_in IN list_of_numbers
);
END blog;
/
php 什麼時候用mysql 存儲過程
實現原理
首先,需要知道怎麼寫mysql存儲過程,了解mysq存儲過程語言,
其次,使用mysql工具創建存儲過程。
最後,通過mysql_query()函數執行mysql變量的設置和mysql存儲過程及返回值。
具體代碼如下:
mysql存儲過程代碼
create procedure pro_name(user_id int)
begin
………
end
2. PHP代碼
$host = “localhost”;
$user = “root”;
$password = “232412”;
$db = “user_db”;
$dblink = mysql_connect($host,$user,$password)or die(“can’t connect to mysql”);
mysql_select_db($db,$dblink)or die(“can’t select user_db”);
$user_id = 1;
$res = mysql_query(“set @a=$user_id”,$dblink);
$res = mysql_query(“call pro_name(@a)”,$dblink);
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的存儲過程,則數據庫中將增加一條新記錄。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/183944.html