我无法根据在变量 $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 教程,但它似乎不起作用。
最佳答案
当您开始按顺序进行错误检查时,这将开始以对您有意义的方式形成。
您的查询执行是正确的,SQL 语句本身应该返回预期的值。您的问题仍然在于从获取的 $row
中错误地传输值到他们在 <td>
中的最终位置标签。
一些需要注意的事情:
- Set PDO into
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
的最后说明:如果您真的打算使用当前日期,则不需要绑定(bind)参数。你可以调用 MySQL 的 CURDATE()
直接在声明中:
SELECT * FROM log WHERE log_datetime <= CURDATE()
关于php pdo 在特定日期获取数据返回空结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20885241/