php - 通过 ODBC Sybase "PARAM datastream"错误的 PDO 准备语句

标签 php pdo odbc sap-ase

我正在尝试将一些旧的 PHP ODBC 查询转换为 PDO 准备语句,但出现错误,我找不到太多相关信息。

错误是:

"[DataDirect][ODBC Sybase Wire Protocol driver][SQL Server]There is no host variable corresponding to the one specified by the PARAM datastream. This means that this variable '' was not used in the preceding DECLARE CURSOR or SQL command. (SQLExecute[3801] at ext\pdo_odbc\odbc_stmt.c:254)"

  • 我正在使用 6 位 ID 在数据库中搜索单个行,该 ID 作为 VARCHAR 存储在数据库中,但通常是 6 位数字。

  • 数据库连接报告成功。

  • 查询字符串传递的 ID 已验证。

  • 准备好的语句导致上述错误。

else 子句中的直接备份 ODBC_EXEC 语句返回我要查找的数据。

//PDO Driver Connect to Sybase
try {
    $pdo = new PDO("odbc:Driver={Sybase ASE ODBC Driver};NA=server,5000;Uid=username;Pwd=password;");
    $pdo_status = "Sybase Connected";
} catch(PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}


if((isset($_GET['id'])) AND ($_GET['id'] != "")) {

//Validate ID String
if(!preg_match("/^[A-Za-z0-9]{5,7}/",$_GET['id'])) {
    $query1_id = FALSE;
    echo "Invalid ID";
    exit;
} else {
    $query1_id = $_GET['id'];
}

$query1 = $pdo->prepare("SELECT * FROM People WHERE PersonId= ?");
$query1->execute(array($query1_id));
if($query1->errorCode() != 0) {
        $person_data = $query1->fetch(PDO::FETCH_ASSOC);    
        echo "Person Data from PDO: ";
        print_r($person_data);
    } else {
        $errors = $query1->errorInfo();
        echo $errors[2];
//Try the old way to confirm data is there.
        $odbc_query1 = "SELECT * FROM People WHERE PersonId='$query1_id' ";
        $person_result = odbc_exec($conn,$odbc_query1) or die("Error getting Data, Query 1");
        $person_data = odbc_fetch_array($person_result);
        echo "Person Data from ODBC_EXEC: ";
        print_r($person_data);
    }

如果我使用它也会失败:

$query1 = $pdo->prepare("SELECT * FROM People WHERE PersonId= :id ");
$query1->execute(array(":id"=>$query1_id));

有没有人遇到过这个错误?

编辑:Sybase Manual says this about the error...

Error 3801: There is no host variable corresponding to the one specified by the PARAM datastream. This means that this variable `%.*s' was not used in the preceding DECLARE CURSOR or SQL command.

Explanation: Adaptive Server could not perform the requested action. Check your command for missing or incorrect database objects, variable names, and/or input data.

这很奇怪,因为我的错误(在顶部引用)没有告诉我哪个变量没有宿主。

如果我使用...也会失败

$query1 = $pdo->prepare("SELECT * FROM People WHERE PersonId= :id ");
$query1->bindParam(':id',$query1_id,PDO::PARAM_STR);  //Or PARAM_INT
$query1->execute();

如果我像这样将变量放在查询中,查询就会工作...

$query1 = $pdo->prepare("SELECT * FROM People WHERE PersonId= '$query1_id'");

所以我认为这与未绑定(bind)到占位符的参数有关,但我不明白为什么。

如果我不能解决这个问题,我将不得不重新构建我的查询作为一个字符串,并希望我的输入验证是防弹的。

最佳答案

您的问题似乎与 PHP 分配给占位符中的变量的默认数据类型有关。 SQL 语句正在寻找一个数字,但 PHP 将其解释为其他内容。您可以使用占位符变量周围的引号来防止这种情况。请注意,在有效的语句中,PHP 看到的值周围有撇号 (''):

$query1 = $pdo->prepare("SELECT * FROM People WHERE PersonId= '$query1_id'");

在使用占位符时尝试这样做,它应该是相同的:

$query1 = $pdo->prepare("SELECT * FROM People WHERE PersonId= ':id'"); 

关于php - 通过 ODBC Sybase "PARAM datastream"错误的 PDO 准备语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13130745/

相关文章:

php - 具有 2 个表和多个条目的 MySQL 查询 JOIN

javascript - .load() 的 .ajax() 等价物是什么?

php - 从 PDO 准备好的语句中获取 SQL 查询和参数

php 在我的时区获取日期

mysql - SQL SERVER ODBC to MYSQL 孪生记录有什么不同?

PHP mysql Xampp 无法打开流 : No such file or directory

php - 加入同一个表?

php - 不能将 PDOStatement 类型的对象用作数组

sql - Excel ODBC 数据连接查询刷新每个查询所需的时间

php - 创建链接服务器到Mysql Cpanel