[英]Error after migrating from Mysql to MariaDb
我有一個奇怪的錯誤:我有那個簡單的代碼:
$id = strip_tags($_SESSION["infos_profile_id"]);
$id_friend = strip_tags($_POST["update_user_chat_every_5_second"]);
$q = $bdd->query('SELECT * FROM message WHERE id_sender = '.$id_friend.' AND id_send_to = '.$id.' AND message_read = "0"');
它在 mysql 上運行良好。
但是在 mariadb 服務器上托管我的網站后,它看到了那個錯誤。
PHP 致命錯誤:未捕獲的異常 'PDOException' 帶有消息 'SQLSTATE[42000]:語法錯誤或訪問沖突:1064 您的 SQL 語法有錯誤; 檢查與您的 MariaDB 服務器版本相對應的手冊,了解在第 1 行的“AND message_read = "0"”附近使用的正確語法
我已經做了一切來解決,但我找不到真正的錯誤來自哪里。
任何幫助解決該錯誤?
謝謝。
我同意上面@MaxT 的評論——您應該在將所有變量插入到 SQL 查詢中后回顯您的 SQL 查詢。 當您查看格式化 SQL 查詢的代碼而不是查詢本身時,調試太困難了。
評論也是正確的, strip_tags()
對 SQL 注入保護沒有用。
查詢參數是防止 SQL 注入的最佳保護措施,它們也可以幫助您避免語法錯誤。
這是您的代碼的樣子:
$id = $_SESSION["infos_profile_id"];
$id_friend = $_POST["update_user_chat_every_5_second"];
$sql = 'SELECT * FROM message WHERE id_sender = :id_friend AND id_send_to = :id AND message_read = 0';
$q = $bdd->prepare($sql);
$q->execute( ['id'=>$id, 'id_friend'=>$id_friend] );
這真的很容易!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.