[英]MySQL SELECT statement COUNT error
EDIT - Using PHP I made an ODBC connection to a local Access database 编辑 - 使用PHP我与本地Access数据库建立了ODBC连接
I'm getting the following error: 我收到以下错误:
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
I have a feeling some of the syntax is wrong here for an ODBC connection 我有一种感觉,这里的一些语法错误的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)=?)";
This statement works fine in Access, but its not translating to MySQL very well. 这个语句在Access中运行良好,但它不能很好地转换为MySQL。 What syntax am i missing here? 我在这里缺少什么语法?
FULL CODE RECENTLY EDITED TO REMOVE PDO STATEMENT : 最近编辑的完整代码以删除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>";
?>
THE error is at this line: `$rs=odbc_exec($conn,$sql); 错误在这一行:`$ rs = odbc_exec($ conn,$ sql);
This code works just fine when I edit the $sql statement to remove the ands: 当我编辑$ 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'";
So the obvious error is the LEFT() functions. 所以明显的错误是LEFT()函数。 Can someone advise on how to approach this? 有人可以建议如何处理这个问题吗? Also, I'm not sure what the =? 另外,我不确定是什么=? means. 手段。
Your problem was that your query included placeholders ( ?
) for a prepared statement, but you were treating it as a normal query. 您的问题是您的查询包含准备好的语句的占位符( ?
),但您将其视为普通查询。 Prepared statements need to be prepared with odbc_prepare()
and then executed with odbc_execute()
. 准备好的语句需要用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);
}
?>
(Who would have guessed there would be a dedicated function for printing a result set as an HTML table!!?) (谁会猜到会有一个专门的函数将结果集打印为HTML表格!!?)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.