【问题标题】:PDO BindValue doesn't work - execute returns nothingPDO BindValue 不起作用 - 执行不返回任何内容
【发布时间】:2015-05-12 06:59:54
【问题描述】:

我在 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 语句)代码就像魅力一样工作!

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

谢谢

【问题讨论】:

  • 即使在类别中没有引号也能正常工作?在 bindValue() 中,您是说类别是一个字符串。
  • 实际上不是 - 写那行只是为了表明没有 bindValue() 执行工作完美并且忘记了示例中的引号。实际的 sql-string 是:SELECT * FROM `woody_sidebar` WHERE `visible`=$firstNumber AND `access`&lt;=$secondNumber AND `category`='$thirdString' ORDER BY `orderNum` ASC

标签: php mysql sql pdo bindvalue


【解决方案1】:

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

试试这个

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();
}

【讨论】:

    【解决方案2】:

    改变这个:

    $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 是INT,可以作为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);
    

    【讨论】:

    • 不仅表名在第一次和第二次查询中不同,变量名也不同......
    • 相应地命名为 $thirdString - 第三个参数应该是字符串,我想是这样......
    • @holzers 你有预付款吗?
    • 我认为我们应该询问这个问题的作者)
    猜你喜欢
    • 1970-01-01
    • 2013-01-03
    • 2011-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-16
    • 2016-01-28
    相关资源
    最近更新 更多