运行 SQL 函数作为 PDO 准备语句和返回值
Running SQL function as PDO prepared statement and returning values
我正在尝试将用户的 IP 地址存储在数据库中,并基于此 Whosebug Question 我了解到 IP 地址应以二进制格式存储。
运行 函数作为准备语句 - 我可以将 @ip 绑定为 $text 参数
$stmt = $this->dbconnect->prepare ('CREATE FUNCTION dbo.fnBinaryIPv4(@:ip AS VARCHAR(15)) RETURNS BINARY(4)
AS
BEGIN
DECLARE @bin AS BINARY(4)
SELECT @bin = CAST( CAST( PARSENAME( @ip, 4 ) AS INTEGER) AS BINARY(1))
+ CAST( CAST( PARSENAME( @ip, 3 ) AS INTEGER) AS BINARY(1))
+ CAST( CAST( PARSENAME( @ip, 2 ) AS INTEGER) AS BINARY(1))
+ CAST( CAST( PARSENAME( @ip, 1 ) AS INTEGER) AS BINARY(1))
RETURN @bin
END;');
$stmt->execute(array(':ip'=>$text));
如何使用返回值 (@bin)?
您的代码不是在执行存储的函数,而是在创建它。如果你想执行一个以前创建的存储函数,只需 select 它是 return value
例如:
$stmt = $this->dbconnect->prepare ('SELECT dbo.fnBinaryIPv4(:ip) as bin');
$stmt->execute(array(':ip'=>$text));
我正在尝试将用户的 IP 地址存储在数据库中,并基于此 Whosebug Question 我了解到 IP 地址应以二进制格式存储。
运行 函数作为准备语句 - 我可以将 @ip 绑定为 $text 参数
$stmt = $this->dbconnect->prepare ('CREATE FUNCTION dbo.fnBinaryIPv4(@:ip AS VARCHAR(15)) RETURNS BINARY(4)
AS
BEGIN
DECLARE @bin AS BINARY(4)
SELECT @bin = CAST( CAST( PARSENAME( @ip, 4 ) AS INTEGER) AS BINARY(1))
+ CAST( CAST( PARSENAME( @ip, 3 ) AS INTEGER) AS BINARY(1))
+ CAST( CAST( PARSENAME( @ip, 2 ) AS INTEGER) AS BINARY(1))
+ CAST( CAST( PARSENAME( @ip, 1 ) AS INTEGER) AS BINARY(1))
RETURN @bin
END;');
$stmt->execute(array(':ip'=>$text));
如何使用返回值 (@bin)?
您的代码不是在执行存储的函数,而是在创建它。如果你想执行一个以前创建的存储函数,只需 select 它是 return value
例如:
$stmt = $this->dbconnect->prepare ('SELECT dbo.fnBinaryIPv4(:ip) as bin');
$stmt->execute(array(':ip'=>$text));