0

我整个早上都在做这个,我不知道为什么它不起作用。

$spn = '20275096';
$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)}; Dbq=".realpath('exported/test1.mdb')."; Uid=Admin");   
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
$stmt = $dbh->prepare(" SELECT Cup_PartUpdateCommodityID FROM Cup_PartUpdateCommodity 
                   INNER JOIN Cup_PartUpdateInfo ON 
                      [Cup_PartUpdateCommodity].Cup_PartUpdateCommodityID = [Cup_PartUpdateInfo].Cup_PartUpdateCommodityID 
                   INNER JOIN Cup_PartUpdate ON 
                      [Cup_PartUpdateInfo].Cup_PartUpdateID = [Cup_PartUpdate].Cup_PartUpdateID 
                   WHERE [Cup_PartUpdate].PartNum = :partnum ");

$stmt->bindParam(':partnum', $spn);
$stmt->execute();
$row = $stmt->fetch(); 
echo $row['Cup_PartUpdateCommodityID'];

它抛出以下错误:

致命错误:未捕获的异常“PDOException”,带有消息“SQLSTATE [42000]:语法错误或访问冲突:-3100

[Microsoft][ODBC Microsoft Access Driver] 查询表达式中的语法错误(缺少运算符)“[Cup_PartUpdateCommodity].Cup_PartUpdateCommodityID = [Cup_PartUpdateInfo].Cup_PartUpdateCommodityID INNER JOIN Cup_PartUpdate ON [Cup_PartUpdateInfo].Cup_PartUpdateID = [Cup_PartUpdate].Cup_PartUpdateID”。(SQLExecute[-3100] at ext\pdo_odbc\odbc_stmt.c:254)' 在 C:\sites\myproject\test.php:31

堆栈跟踪:#0 C:\sites\myproject\test.php(31): PDOStatement->execute() #1 {main} 在第 31 行的 C:\sites\myproject\test.php 中抛出

注意:上面的代码将在一个需要 $spn 作为参数的函数中执行。就目前而言,我正在为 $spn 设置默认值。任何人?

4

1 回答 1

0

终于找到了基于的解决方案。

事实证明,MS Access 的 JOINS 需要像这样完成:

A JOIN (
B JOIN C ON <condition_BC>
) ON <condition_AB>

而不是像 MySQL 的方式:

A JOIN B ON <condition_AB>
JOIN C ON <condition_BC>

所以最后我的查询看起来像这样:

$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)}; Dbq=".realpath('exported/test1.mdb')."; Uid=Admin");   
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    $stmt = $dbh->prepare(" SELECT * FROM Cup_PartUpdateCommodity 
                            LEFT JOIN (
                              Cup_PartUpdateInfo LEFT JOIN Cup_PartUpdate ON Cup_PartUpdateInfo.Cup_PartUpdateID = Cup_PartUpdate.Cup_PartUpdateID 
                            ) ON Cup_PartUpdateCommodity.Cup_PartUpdateCommodityID = Cup_PartUpdateInfo.Cup_PartUpdateCommodityID
                            WHERE Cup_PartUpdate.PartNum = :partnum ");
    $stmt->bindParam(':partnum', $spn);
    $stmt->execute();
    $row = $stmt->fetch(); 
    echo $row['Cup_PartUpdateCommodityID'];
于 2013-09-14T17:00:30.397 回答