【问题标题】:ODBC can't filter data with parameterODBC 无法使用参数过滤数据
【发布时间】:2014-07-23 10:23:14
【问题描述】:

我使用 ODBC 通过 Web 服务将我的 Ms SQL Server 数据库与我的 Android 应用程序连接起来。 我尝试使用 PHP 脚本获取数据,它运行平稳以从我的 Ms SQL Server 数据库中获取所有数据。但是当我尝试在我的 php 脚本中使用“WHERE”子句获取一些数据并在我的查询中添加参数时,我得到了错误。这是我的 php 脚本:

<?php
$dsn="myDSN";
$username="myUSername";
$password="myPassword";

$barcode = $_GET['barcode'];

$koneksi = odbc_connect($dsn,$username,$password);

$sql = "select Barcode_ID, WO, ProductCategory, SubProductName, OrderType from T_MAIN 
    where Barcode_ID = $barcode";

$respon = array();

$tbl_main = odbc_exec($koneksi, $sql);

if (odbc_num_rows($tbl_main) > 0) {
    $respon["data_main"] = array();

    while ($row = odbc_fetch_array($tbl_main)) {
        $data_main = array();
        $data_main["barcodeID"] = $row["Barcode_ID"];
        $data_main["wo"] = $row["WO"];
        $data_main["product"] = $row["ProductCategory"];
        $data_main["subProduct"] = $row["SubProductName"];
        $data_main["ordertype"] = $row["OrderType"];

        array_push($respon["data_main"], $data_main);
    }
    $respon["sukses"] = 1;
    $respon["pesan"] = "Success!";

    echo json_encode($respon);
} else {
    $respon["gagal"] = 0;
    $respon["pesan"] = "Failed!";

    echo json_encode($respon);
}
odbc_close($koneksi);
?>

我在浏览器中运行它,我得到这个错误:

Warning: odbc_exec(): SQL error: [Microsoft][ODBC SQL Server Driver]COUNT field incorrect or syntax error, SQL state 07001 in SQLExecDirect in C:\xampp\htdocs\myscripts\get_data_main.php on line 15

Warning: odbc_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\myscript\get_data_main.php on line 19 {"gagal":0,"pesan":"Gagal Mengakses Database!"}

有人可以帮我解决这个问题吗? 非常感谢您的任何建议。

【问题讨论】:

  • 你需要在你的 sql 中转义字符串字符:where Barcode_ID = '$barcode'
  • 我已经尝试过了,但是不行,因为 Bacode_ID 是整数

标签: php android sql-server parameters odbc


【解决方案1】:

使用参数化查询,例如“select Barcode_ID, WO, ProductCategory, SubProductName, OrderType from T_MAIN where Barcode_ID = ?”然后将 $barcode 传递给 execute 方法。

【讨论】:

【解决方案2】:

感谢您的所有建议。我已经解决了这个问题。警告显示“SQLExecDirect 中的 SQL 状态 07001”,当我搜索它时,我得到它“错误的数字参数”。所以我试试这段代码: $sql = "select Barcode_ID, WO, ProductCategory, SubProductName, OrderType from T_MAIN where Barcode_ID = '".(int)$barcode."'"; 并且它的作品完美。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-07
    • 2023-04-04
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多