MySQL SELECT语句COUNT错误

时间:2014-02-20 22:16:17

标签: php mysql sql database ms-access

编辑 - 使用 PHP 我与本地Access数据库建立了ODBC连接

我收到以下错误:

Warning: odbc_exec(): SQL error: [Microsoft][ODBC Microsoft Access Driver]COUNT field incorrect , SQL state 07001 in SQLExecDirect in C:\xampp\htdocs\cpanel\modelsearch\test.php on line 37 Error in SQL

我觉得ODBC连接的一些语法错误

$sql="SELECT `ITEM`, `DESCRIPTION`, `CUS_LOC_NAME`, `MECH_REL_BY`, `PM_DESIGN`, `SHIP_ACTUAL`, `HPL`, `WO_LINE`, `SO` FROM `Schedule` WHERE `HPL` <> 'PART' AND (LEFT(`DESCRIPTION`,6)=?) AND (LEFT(`CUS_LOC_NAME`,6)=?)";

这个语句在Access中运行良好,但它不能很好地转换为MySQL。我在这里缺少什么语法?

完整代码最近开始删除PDO声明

<?php
date_default_timezone_set('America/Chicago');

 $sql="SELECT `ITEM`, `DESCRIPTION`, `CUS_LOC_NAME`, `MECH_REL_BY`, `PM_DESIGN`, `SHIP_ACTUAL`, `HPL`, `WO_LINE`, `SO` FROM `Schedule` WHERE `HPL` <> 'PART' AND (LEFT(`DESCRIPTION`,6)=?) AND (LEFT(`CUS_LOC_NAME`,6)=?)";
 $rs=odbc_exec($conn,$sql);
 if (!$rs) {
   exit("Error in SQL");
 } 

 echo "<table><tr>";
 echo "<th>ITEM</th>";
 echo "<th>DESCRIPTION</th>";
 echo "<th>CUS_LOC_NAME</th>";
 echo "<th>MECH_REL_BY</th>";
 echo "<th>PM_DESIGN</th>";
 echo "<th>SHIP_ACTUAL</th>";
 echo "<th>HPL</th>";
 echo "<th>WO_LINE</th>";
 echo "<th>SO</th></tr>";

 while (odbc_fetch_row($rs)) {
   $item=odbc_result($rs,"ITEM");
   $desc=odbc_result($rs,"DESCRIPTION");
   $cus=odbc_result($rs,"CUS_LOC_NAME");
   $mech=odbc_result($rs,"MECH_REL_BY");
   $pm_design=odbc_result($rs,"PM_DESIGN");
   $ship=odbc_result($rs,"SHIP_ACTUAL");
   $hpl=odbc_result($rs,"HPL");
   $wo=odbc_result($rs,"WO_LINE");
   $so=odbc_result($rs,"SO");

   echo "<tr><td>$item</td>";
   echo "<td>$desc</td>";
   echo "<td>$cus</td>";
   echo "<td>$mech</td>";
   echo "<td>$pm_design</td>";
   echo "<td>$ship</td>";
   echo "<td>$hpl</td>";
   echo "<td>$wo</td>";
   echo "<td>$so</td></tr>";
 }
odbc_close($conn);
echo "</table>";


?>

错误在这一行:`$ rs = odbc_exec($ conn,$ sql);

当我编辑$ sql语句以删除ands:

时,此代码工作正常
$sql="SELECT `ITEM`, `DESCRIPTION`, `CUS_LOC_NAME`, `MECH_REL_BY`, `PM_DESIGN`, `SHIP_ACTUAL`, `HPL`, `WO_LINE`, `SO` FROM `Schedule` WHERE `HPL` <> 'PART'";

所以明显的错误是LEFT()函数。有人可以建议如何处理这个问题吗?另外,我不确定是什么=?装置

1 个答案:

答案 0 :(得分:2)

您的问题是您的查询包含准备好的语句的占位符(?),但您将其视为普通查询。准备好的陈述需要使用odbc_prepare()准备,然后使用odbc_execute()执行。

<?php
date_default_timezone_set("America/Chicago");

$conn = odbc_connect("Prod_Schedule", "", "");
if (!$conn) {
    exit("Connection Failed: $conn");
}

$sql="SELECT `ITEM`, `DESCRIPTION`, `CUS_LOC_NAME`, `MECH_REL_BY`, `PM_DESIGN`, `SHIP_ACTUAL`, `HPL`, `WO_LINE`, `SO` FROM `Schedule` WHERE `HPL` <> 'PART' AND (LEFT(`DESCRIPTION`,6)=?) AND (LEFT(`CUS_LOC_NAME`,6)=?)";

$stmt = odbc_prepare($conn, $sql);
$params = array("value for first ?", "value for second ?");
$result = odbc_execute($stmt, $params);

if ($result) {
    odbc_result_all($stmt);
}
?>

(谁会猜到会有一个专门的函数将结果集打印为HTML表格!!?)