【发布时间】: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