简体   繁体   中英

What's wrong with this php pdo code?

The foreach won't echo anything out, anybody knows what's wrong with it?

Note: $checks is the value of checkboxes.

<?php
    $hostname = 'localhost';
    $username = 'root';
    $password = '';
    $database = 'foo';

    try{

        $db = new PDO("mysql:host=$hostname;dbname=$database",$username,$password);

        $checks = $_POST['checks'];

        $post = "SELECT * FROM articles WHERE title= ' ". $checks ." '";

        foreach($db->query($post) as $row){
            echo $row['title'] . '' . $row['body'];
        }

    }catch(PDOException $e){
        echo 'CONNECTION UNSUCCESFUL!';
    }

?>

Extra spacing around the title? So the title with the extra spaces around it never gets a match from the your database.

WHERE title= ' ". $checks ." '";

Change to

WHERE title= '". $checks ."'";

and try again.

$post = "SELECT * FROM articles WHERE title IN (' ". implode("','", $checks) ." ') ";

如果$ checks是数组,那么你应该这样使用

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