0

我在 pdo 上遇到了一个大问题,似乎没有人能帮助我 - 所以我决定问你们 :-)

try {
    $links = $database->prepare("SELECT * FROM aTable WHERE visible=:visible AND access<=:access AND category=:category ORDER BY orderNum ASC");
    $links->bindValue(':visible',$first,PDO::PARAM_INT);
    $links->bindValue(':access',$second,PDO::PARAM_INT);
    $links->bindValue(':category',$third,PDO::PARAM_STR);
    $links->execute();
    print_r($asdf);
    print_r($database->errorInfo());
    print_r($links->errorInfo());
    while($row = $links->fetch(PDO::FETCH_ASSOC)){
        print_r($row);
    }
} catch (PDOException $e) {
    echo $e->getMessage();
}

Database-Connection 完美运行,errorInfo() 都返回:

Array
(
    [0] => 00000
    [1] => 
    [2] => 
)

现在,这段代码不知何故没有得到任何 $row's。但是,如果我将准备语句替换为以下几行:

$sql = "SELECT * FROM woody_sidebar WHERE visible=$first AND access<=$second AND category=$third ORDER BY orderNum ASC";
$links = $database->prepare($sql);

(并删除 bindValue 语句)代码就像魅力一样工作!

我不知道我做错了什么,因为根本没有抛出错误 - 你们中有人知道我可以尝试什么吗?

谢谢

4

2 回答 2

0

改变这个:

$links = $database->prepare("SELECT * FROM aTable WHERE visible=:visible AND access<=:access AND category=:category ORDER BY orderNum ASC");
$links->bindValue(':visible',$first,PDO::PARAM_INT);
$links->bindValue(':access',$second,PDO::PARAM_INT);
$links->bindValue(':category',$third,PDO::PARAM_STR);

对此:

在第二个查询中,您的表名是woody_sidebar,但您有aTable并且 $third 是 aINT并且可以作为 a 传递PDO::PARAM_INT

$links = $database->prepare("SELECT * FROM woody_sidebar WHERE visible=:visible AND access<=:access AND category=:category ORDER BY orderNum ASC");
$links->bindValue(':visible',$first,PDO::PARAM_INT);
$links->bindValue(':access',$second,PDO::PARAM_INT);
$links->bindValue(':category',$third,PDO::PARAM_INT);
于 2015-03-13T19:24:30.963 回答
0

我看到的唯一问题是您的表名。

试试这个

try {
    $sql = "SELECT * FROM woody_sidebar WHERE visible=:visible AND access<=:access AND category=:category ORDER BY orderNum ASC";
    $links = $database->prepare($sql);
    $links->bindValue(':visible', $first, PDO::PARAM_INT); // assuming this is an integer
    $links->bindValue(':access', $second, PDO::PARAM_INT); // assuming this is an integer
    $links->bindValue(':category', $third, PDO::PARAM_STR); // assuming this is an text string or date
    $links->execute();
    print_r($asdf);
    print_r($database->errorInfo());
    print_r($links->errorInfo());
    while($row = $links->fetch(PDO::FETCH_ASSOC)){
        print_r($row);
    }
} catch (PDOException $e) {
    echo $e->getMessage();
}
于 2015-09-13T04:25:04.343 回答