-2

试图搬到 PDO 并度过一段美好的时光。这是我的代码,它工作不正常:

$db = new PDO('mysql:host=localhost;dbname=pp_production;charset=utf8', 'mr', 'jones');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

$report_date = "DATE(time_track.punch_in) = CURRENT_DATE ORDER BY time_track.punch_in DESC";

$query_clock_history = $db->prepare("SELECT time_track.id, time_track.job_id, first_name, activity, time_track.comments, time_track.punch_in, time_track.punch_out, TIMESTAMPDIFF(MINUTE, time_track.punch_in, time_track.punch_out) AS time_spent, customers.customer_name, typesetting.description, typesetting.job_ticket FROM typesetting 
JOIN time_track ON typesetting.id = time_track.job_id 
JOIN employees ON employees.id = time_track.employee_id 
JOIN activities ON activities.id = time_track.activity_id 
JOIN customers ON customers.id = typesetting.customers_id 
WHERE :report_date");
$query_clock_history->bindValue(':report_date', $report_date, PDO::PARAM_STR);
$query_clock_history->execute();

因此,这不会返回任何结果。如果我将 :report_date 替换为 $report_date 变量的实际内容,它就可以工作。一段时间以来,我一直在努力解决这个问题,但无法让它发挥作用。非常感谢任何帮助。这是在 MAMP 上运行的,如果有什么不同的话。

尝试了一些不起作用的东西

$report_date = "CURRENT_DATE";
...
WHERE DATE(time_track.punch_in) = :report_date ORDER BY time_track.punch_in DESC");

依然没有。这似乎是一个简单的例子,但我不知道为什么它不起作用。

4

2 回答 2

0

您不能绑定任意查询部分,只能绑定标量值(即字符串和数字),无论是在 PDO 还是 mysqli 中

不过,您根本不需要对此查询进行绑定

$sql = "SELECT time_track.id, time_track.job_id, first_name, activity, time_track.comments, time_track.punch_in, time_track.punch_out, TIMESTAMPDIFF(MINUTE, time_track.punch_in, time_track.punch_out) AS time_spent, customers.customer_name, typesetting.description, typesetting.job_ticket FROM typesetting 
JOIN time_track ON typesetting.id = time_track.job_id 
JOIN employees ON employees.id = time_track.employee_id 
JOIN activities ON activities.id = time_track.activity_id 
JOIN customers ON customers.id = typesetting.customers_id 
WHERE DATE(time_track.punch_in) = CURRENT_DATE ORDER BY time_track.punch_in DESC"

$stmt = $db->prepare($sql);
$stmt->execute();

就这样

于 2013-05-16T06:04:49.860 回答
0

你的 where 必须有一个条件,即

WHERE date = :report_date

来自php网站

$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
    FROM fruit
    WHERE calories < :calories AND colour = :colour');
$sth->bindParam(':calories', $calories, PDO::PARAM_INT);
$sth->bindParam(':colour', $colour, PDO::PARAM_STR, 12);
$sth->execute();

因此,您的 :report_date 参数需要在查询语句中针对表中的列有一个条件

于 2013-05-16T06:11:15.857 回答