【问题标题】:Inserting data into SQL Server Db An Invalid direction was specified将数据插入 SQL Server Db 指定了无效的方向
【发布时间】:2019-01-05 09:24:57
【问题描述】:

我正在使用 SQL Server DB (mssql),并尝试通过 PHP 执行插入查询。

public function registerCustomer($custId, $custData)
{

    $sqlString = "INSERT INTO CUSTMASTER ( CustId, CustData ) values ( ?, CONVERT(varbinary(max),? )) ; SELECT SCOPE_IDENTITY()";
    $params = array($custId, $custData);
    $stmt = sqlsrv_query($this->conn, $sqlString, $params);
    if ($stmt === false) {
        die(print_r(sqlsrv_errors(), true));
    }

    $rows = sqlsrv_has_rows($stmt);
    sqlsrv_next_result($stmt);
    sqlsrv_fetch($stmt);
    $lastId = sqlsrv_get_field($stmt, 0);

    if ($rows === true) {
       echo "Data Inserted."
    } else {
        return "Data Insert Failed.";
    }

}

我收到以下错误,

Array
(
    [0] => Array
        (
            [0] => IMSSP
            [SQLSTATE] => IMSSP
            [1] => -15
            [code] => -15
            [2] => An invalid direction for parameter 2 was specified. SQLSRV_PARAM_IN, SQLSRV_PARAM_OUT, and SQLSRV_PARAM_INOUT are valid values.
            [message] => An invalid direction for parameter 2 was specified. SQLSRV_PARAM_IN, SQLSRV_PARAM_OUT, and SQLSRV_PARAM_INOUT are valid values.
        )
)

注意:db 中的 CustData 列是 varbinary(max) 类型。

我正在从 Android 应用程序发送 Base64 字符串,然后使用以下代码将字符串转换为 byte[]。

 $a = base64_decode($FingerData);
 $custData = array();
 foreach (str_split($a) as $c) {
    $custData[] = sprintf("%08b", ord($c));
 }

如何解决这个问题?

【问题讨论】:

  • 也发布您的$params 数组代码,以便我们查看问题所在
  • @IgorIlic:嗨。你的意思是发布实际值?

标签: php sql-server sqlsrv varbinary varbinarymax


【解决方案1】:

如果我理解正确,您有一个 base64 加密图像。所以试试这个:

<?php 

# Only decode $FingerData.
...
$a = base64_decode($FingerData);
...

# Insert data
public function registerCustomer($custId, $custData)
{
    ...
    $sqlString = "INSERT INTO CUSTMASTER (CustId, CustData) VALUES (?, CONVERT(varbinary(max), ?)); SELECT SCOPE_IDENTITY();";
    $params = array(
        array($custId, SQLSRV_PARAM_IN),
        array($custData, SQLSRV_PARAM_IN)
    );
    $stmt = sqlsrv_query($this->conn, $sqlString, $params);
    ...
}
?>

我根据您的示例制作了一个小脚本:

<?php
#------------------------------
# Connection info
#------------------------------
$server = 'server\instance,port';
$database = 'database';
$uid = 'user';
$pwd = 'password';

#------------------------------
# Connection
#------------------------------
$cinfo = array(
    "Database" => $database,
    "UID" => $uid,
    "PWD" => $pwd
);
$conn = sqlsrv_connect($server, $cinfo);
if( $conn === false )
{
    echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
    exit;
}

#------------------------------
# Function
#------------------------------
function registerCustomer($connection, $custId, $custData) {

    $sqlString = "INSERT INTO CUSTMASTER (CustId, CustData) VALUES (?, CONVERT(varbinary(max), ?)); SELECT SCOPE_IDENTITY();";
    $params = array(
        array($custId, SQLSRV_PARAM_IN),
        array($custData, SQLSRV_PARAM_IN)
    );
    $stmt = sqlsrv_query($connection, $sqlString, $params);
    if ($stmt === false) {
        die(print_r(sqlsrv_errors(), true));
    }

    $rows = sqlsrv_has_rows($stmt);
    while (sqlsrv_fetch($stmt)) {
        $lastId = sqlsrv_get_field($stmt, 0);
    }

    if ($rows === true) {
       echo "Data Inserted.";
    } else {
        return "Data Insert Failed.";
    }

    echo $lastId;
}

#------------------------------
# Load image file into database
#------------------------------
# With next two lines I load and encode an image. 
# If I understand you correctly, this is already done and you have a base64 encrypted image.
$image = file_get_contents('image.jpg');
$encoded_image = base64_encode($image);

# Insert image
$decoded_image = base64_decode($encoded_image);
registerCustomer($conn, 1, $decoded_image);
?>

【讨论】:

  • 实际上在此处发布问题时错过了该行。它最初在代码中。
  • @javabee 我已经更新了答案。尝试使用完整的语法。
  • 您好 更改后出现此错误。 Array ( [0] => Array ( [0] => IMSSP [SQLSTATE] => IMSSP [1] => -16 [code] => -16 [2] => 为参数 1 指定了无效的 PHP 类型。 [message] => 为参数 1 指定了无效的 PHP 类型。)
  • @javabee $custID 和 $custData 的类型是什么?
  • custId 是 int 并且 custData 是 byte[ ]。 custId 也不是自动生成的或主键..所以这就是我在插入查询中传递它的原因。
【解决方案2】:

我遇到了错误An invalid direction for parameter # was specified 并找到了这个问题。我的问题,假设是 OP 问题,是位置 # 的变量是一个数组。 sqlsrv_query 正在尝试将参数解析为值方向数组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 2020-03-27
    • 2012-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-12
    相关资源
    最近更新 更多