[英]What's wrong with this php pdo code?
The foreach won't echo anything out, anybody knows what's wrong with it? foreach不会回声任何东西,有人知道它怎么了吗?
Note: $checks is the value of checkboxes. 注意:$ checks是复选框的值。
<?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是数组,那么你应该这样使用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.