問題描述
所以我有一個(gè)使用存儲(chǔ)過程與我的 SQL 數(shù)據(jù)庫(kù)交互的 php 腳本.存儲(chǔ)過程工作得很好,問題是我不知道如何讓我的 php 從存儲(chǔ)過程中回顯返回值.存儲(chǔ)過程基本上是使用激活密鑰激活帳戶并設(shè)置用戶名和密碼.
So I have a php script that uses a stored procedure to interact with my SQL database. The stored procedure works just fine, the problem is I don't know how to get my php to echo the Return Value from the stored procedure. The stored procedure is basically activating an account using an activation key and sets the username and password.
它基本上是說如果提供的激活密鑰還沒有用戶名,請(qǐng)將其設(shè)置為提供的用戶名并返回 1,如果它已經(jīng)有用戶名返回 2,如果激活密鑰不存在返回 3".它在 SQL 中完美運(yùn)行,甚至可以提供正確的返回值.現(xiàn)在我怎樣才能讓我的 php 回應(yīng)呢?我嘗試了以下方法:
It basically says "if the activation key provided does not have a username yet, set it to the one provided and RETURN 1, if it already has a username RETURN 2, and if the activation key does not exist RETURN 3". It works perfectly in SQL and even give the correct Return Value. Now how can I get my php to echo that? I have tried the following:
$link = sqlsrv_connect($myServer, array('Database' => $myDB, 'UID' => $myUser, 'PWD' => $myPass));
if($link === FALSE) {
die('Could not connect: ' . sqlsrv_errors(SQLSRV_ERR_ALL));
}
$key = $_REQUEST['key'];
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
$query = "EXEC Activate_Account";
$query .=" @key = ";
$query .= $key;
$query .=", @user = ";
$query .= $username;
$query .=", @pass = ";
$query .= $password;
$result = sqlsrv_query($link, $query);
while($row = sqlsrv_fetch_array($result))
{
echo $row[0];
}
sqlsrv_close($link);
和
while($row = sqlsrv_fetch_array($result))
{
echo $row['Return Value'];
}
他們都沒有回音.
推薦答案
使用存儲(chǔ)過程返回值:
例如:
To return a value with a stored procedure:
For example:
SQL:
CREATE DEFINER=`user`@`localhost` PROCEDURE `ProcedureName`(IN `Input_Value` INT, OUT `Out_val` INT)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
// Your SQL Code
SET Out_val= Your Value;
SELECT Out_val;
END
PHP 代碼:
$insert = "CALL ProcedureName(:Input_Value,
@Out_val)";
$bdd = new PDO('mysql:host=localhost;dbname=db-name', 'user', 'password');
$stmt = $bdd->prepare($insert);
$stmt->bindParam(':Input_Value', $an_input_value, PDO::PARAM_STR);
$stmt->execute();
$tabResultat = $stmt->fetch();
$Out_val = $tabResultat['Out_val'];
var_dump($Out_val);
這篇關(guān)于使用 PHP 從 SQL 存儲(chǔ)過程中獲取返回值的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!