如何使用PDO在WHERE之后使用两个参数进行选择


How to do a select with two parameters after the WHERE using PDO?

我试图从数据库中选择数据,但当WHERE后面有两个参数时,我无法获得它。

有效代码:

$conn = null;
$host = 'localhost';
$db   = 'database';
$user = 'root';
$pwd  = 'root';
$auth = 'EP';
$nr = 2007;
try {
    $conn = new 'PDO('mysql:host='.$host.';dbname='.$db, $user, $pwd);      
    $stmt = $conn->prepare('SELECT family FROM table WHERE nr = :nr');
    $stmt->execute(array('nr' => $nr));
    while($row = $stmt->fetch(PDO :: FETCH_ASSOC)) {
        echo '<pre>';
        print_r($row);
        echo '</pre>';
    }
}
catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}

但当我使用以下选择时,它不起作用:

SELECT family FROM table WHERE auth = :auth AND nr = :nr

我认为线路有问题

    $stmt->execute(array('nr' => $nr));

当我做以下操作时,屏幕上没有结果:

    $stmt->execute(array('nr' => $nr, 'auth' => $auth));

有人知道我做错了什么吗?

更改此

stmt->execute(array('nr' => $nr, 'auth' => $auth));

$stmt->execute(array(':nr' => $nr, ':auth' => $auth));

:是一个小的打字错误,但PDO是严重的:(并且参数将为空。