我根据在变量$now
以下是我的代码:
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
// bind column to variable
//Connect to DB
$host = "localhost"; //Host Name
$port = '3306'; //Default MySQL Port
$dbname = "mama"; //Database Name
$db_username = "root"; //MySQL Username
$db_password = ""; //MySQL Password
$dsn = "mysql:host=$host;port=$port;dbname=$dbname"; //Data Source Name = Mysql
$db = new PDO($dsn, $db_username, $db_password); //Connect to DB
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
$now = "2014-01-01";
//foreach ($query=$db->query("SELECT * FROM log WHERE log_datetime <= :now") as $row) {print_r($row);}
$query->bindParam(':now', $now, PDO::PARAM_STR);
$success = $query->execute();
}
// Catch any exception thrown
catch (PDOException $e) {
echo "Error: " . $e->getMessage();
// Exit, redirect, whatever you need to do.
}
$rows = $query->fetchAll(PDO::FETCH_OBJ);
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">
table, td {
border: 1px solid black;
border-collapse: collapse;
}
</style>
<title>Test</title>
</head>
<body>
<table>
<th>Id</th>
<th>DOB</th>
<th>Visit</th>
<th>Qty</th>
<th>Amount</th>
<?php
// Use a foreach over $rows
foreach ($rows as $row) {
// Using htmlspecialchars() on these as good practice for HTML escaping
// Since they were fetched via FETCH_OBJ access them as properties with ->
echo '<tr>';
echo '<td>' . htmlspecialchars($row->log_id) . '</td>';
echo '<td>' . htmlspecialchars($row->dob) . '</td>';
echo '<td>' . htmlspecialchars($row->log_datetime) . '</td>';
echo '<td>' . htmlspecialchars($row->log_count) . '</td>';
echo '<td>' . htmlspecialchars($row->amount) . '</td>';
echo '</tr>';
}
?>
</table>
</body>
</html>
我希望结果显示在特定的日期设置上,但除了表头之外什么也不返回。
其他信息,我的log_datetime
使用DATETIME mysql格式。
我做了一些SO搜索,但没有在SO中发布的问题或答案可以帮助我的问题。 请告诉我哪里出错了,我按照php PDO教程进行了操作,但似乎没有用。
答案 0 :(得分:1)
您的SQL语法中有一个小错误:
SELECT * FROM log WHERE log_datetime <= ?
// ----------------------------------^
答案 1 :(得分:1)
错误是在sql语法中,尝试更改
"SELECT * FROM log WHERE log_datetime = < ?"
同
"SELECT * FROM log WHERE log_datetime <= ?"
答案 2 :(得分:0)
当您开始按顺序检查错误时,这将以一种有意义的方式开始形成。
您的查询执行是正确的,SQL语句本身应该返回预期的值。您的问题仍然在于将值从提取的$row
错误地传输到<td>
标记中的最终位置。
要注意的一些事情:
ERRMODE_EXCEPTION
因此会在任何错误上抛出异常。try/catch
中的所有数据库内容包装起来以捕获任何PDO异常htmlspecialchars()
转义HTML的所有输出catch {}
区块require('./inc/connection.php');
// Suggest setting PDO into ERRMODE_EXCEPTION so it throws exceptions on any error
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// With PDO in exception mode, wrap all of this in try/catch
try {
$now = "2014-01-01";
$query = $db->prepare("SELECT * FROM log WHERE log_datetime <= :now");
$query->bindParam(':now', $now);
$success = $query->execute();
}
// Catch any exception thrown
catch (PDOException $e) {
echo "Error: " . $e->getMessage();
// Exit, redirect, whatever you need to do.
}
// Assuming no failure (you didn't exit or redirect)
// You can build your table.
// First fetch all rows:
$rows = $query->fetchAll(PDO::FETCH_OBJ);
// Now all your rows are in the array. You're done with PDO.
echo '<table>';
echo '<tr>
<th>Id</th>
<th>DOB</th>
<th>Visit</th>
<th>Qty</th>
<th>Amount</th>
</tr>';
// Use a foreach over $rows
foreach ($rows as $row) {
// Using htmlspecialchars() on these as good practice for HTML escaping
// Since they were fetched via FETCH_OBJ access them as properties with ->
echo '<tr>';
echo '<td>' . htmlspecialchars($row->log_id) . '</td>';
echo '<td>' . htmlspecialchars($row->dob) . '</td>';
echo '<td>' . htmlspecialchars($row->log_datetime) . '</td>';
echo '<td>' . htmlspecialchars($row->log_count) . '</td>';
echo '<td>' . htmlspecialchars($row->amount) . '</td>';
echo '</tr>';
}
echo '</table>';
// You're done.
我删除了内联style-
属性的所有。这些都不属于您的标记,而应该在CSS中。它可以用CSS规则处理,如:
table, td {
border: 1px solid black;
border-collapse: collapse;
}
关于$now
的最后一点说明:如果您真的打算使用当前日期,则不需要绑定参数。您可以直接在语句中调用MySQL的CURDATE()
:
SELECT * FROM log WHERE log_datetime <= CURDATE()
答案 3 :(得分:-1)
while ($query->fetchAll()) {
echo '<tr>';
echo '<td style="border: 1px solid black;">' . $log_id . '</td>';
echo '<td style="border: 1px solid black;">' . $dob . '</td>';
echo '<td style="border: 1px solid black;">' . $log_datetime . '</td>';
echo '<td style="border: 1px solid black;">' . $log_count . '</td>';
echo '<td style="border: 1px solid black;">' . $amount . '</td>';
echo '</tr>';
}
这不会取任何东西,你需要像
那样重写它while ($row=$query->fetch(PDO::FETCH_OBJ)) {
$log_id = $row->log_id;
$dob = $row->dob;
$log_datetime = $row->log_datetime;
$log_count = $row->log_count;
$amount = $row->amount;
echo '<tr>';
echo '<td style="border: 1px solid black;">' . $log_id . '</td>';
echo '<td style="border: 1px solid black;">' . $dob . '</td>';
echo '<td style="border: 1px solid black;">' . $log_datetime . '</td>';
echo '<td style="border: 1px solid black;">' . $log_count . '</td>';
echo '<td style="border: 1px solid black;">' . $amount . '</td>';
echo '</tr>';
}