当我对DB执行此查询时:
SELECT * FROM `task` WHERE `date_time_from` like '%0000%'
我得到了一些结果,现在我试图用PDO做同样的事情,我无法得到任何结果或错误。这就是我所做的:
$dbChain = 'mysql:host='.$GLOBALS['dbhost'].';dbname='.$GLOBALS['dbname'];
try{
$dbh = new PDO($dbChain, $GLOBALS['dbuser'], $GLOBALS['dbpassword']);
$sql = "SELECT * FROM task"
. "WHERE date_time_from like CONCAT('%', :dateFrom, '%')";
$a = '0000';
$stmt = $dbh->prepare($sql);
$stmt->bindParam(':dateFrom', $a);
$stmt->execute();
$total = $stmt->rowCount();
echo $total;
while ($row = $stmt->fetch()){
var_dump($row);
}
} catch (Exception $e){
echo 'Error'.$e->getMessage();
}
结果是$total = 0
。谁能告诉我我做错了什么?
我也试过这个:
$sql = "SELECT * FROM task"
. "WHERE date_time_from like :dateFrom";
$a = "%0000%";
$stmt = $dbh->prepare($sql);
$stmt->bindParam(':dateFrom', $a);
$stmt->execute();
$total
的结果相同。
答案 0 :(得分:1)
bindParam转义"%"在查询中。它不会像你期望的那样工作......
但是,你可以像这样使用bindValue ......
print getattr(i, 'from').username
或者,如果您想要来自日期时间列的0值,则可以执行以下操作:
$sql = "SELECT * FROM task WHERE date_time_from LIKE ?";
$stmt = $dbh->prepare($sql);
$stmt->bindValue( 1, "%0000%" );
$stmt->execute();