我正在尝试在数据库中存储用户的IP地址,并基于此Stackoverflow Question我了解到IP地址应以BINARY格式存储。
将函数作为预准备语句运行 - 我可以将@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)?
答案 0 :(得分:0)
使用您的代码,您不是在执行存储的函数,而是在创建它。如果要执行以前创建的存储函数,只需选择它的返回值
即可例如:
$stmt = $this->dbconnect->prepare ('SELECT dbo.fnBinaryIPv4(:ip) as bin');
$stmt->execute(array(':ip'=>$text));