简体   繁体   中英

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

I try to select data from a database, but I'm unable to get it when I have two parameters after the WHERE.

Code that works:

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

but when I use following select it doesn't work:

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

I think it's a problem in the line

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

When I do the following I have no result on the screen:

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

Has anybody an idea what I'm doing wrong?

CHANGE THIS

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

TO

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

: is a small typo error but PDO is Serious :( and the parameters will be empty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM