我正在創建一個顯示登錄歷史記錄的頁面。我的代碼當前顯示所有日志,并且應該只顯示登錄用戶的日志歷史記錄。
資料庫 -> 日志:
log_id | user_email | ip_address | time
------- ------------ ------------ -----
1 | ml@a.com | 123.13.13 | 1:30
LogHistory.php 頁面:
<?php
$stmt = $dbh->prepare("SELECT * FROM Logs ORDER BY log_id ASC");
$stmt->execute();
if ($stmt->rowCount() == 0) {
echo 'Log history are empty.';
} else {
// Data we collected from the registered user
}
?>
我試過這段代碼:
<?php
$LoggedInUser = $_SESSION['user'];
$stmt = $dbh->prepare("SELECT * FROM Logs WHERE user_email = $LoggedInUser ORDER BY log_id ASC");
$stmt->execute();
if ($stmt->rowCount() == 0) {
echo 'Log history are empty.';
} else {
// Data we collected from the registered user
}
?>
使用上面的代碼,我收到此錯誤訊息:
PHP Fatal error: Uncaught PDOException: SQLSTATE\[42000\]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ':[email protected] ORDER BY log_id ASC'
uj5u.com熱心網友回復:
您只需將引數與準備好的陳述句一起使用
<?php
$LoggedInUser = $_SESSION['user'];
$stmt = $dbh->prepare("SELECT * FROM Logs WHERE user_email = ? ORDER BY log_id ASC");
$stmt->execute([$LoggedInUser]);
if ($stmt->rowCount() == 0) {
echo 'Log history are empty.';
} else {
// Data we collected from the registered user
}
?>
這里有現場 PHP 代碼
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/527198.html
標籤:phpsql
