我正在使用此代碼>>>
$recipe_counting = $wpdb->get_var(
"SELECT COUNT(*)
FROM '" . $wpdb->prefix . "recipe_ratings'
WHERE recipe_id ='" . $post_ID . "' AND user_ip='" . $user_IP . "'"
);
在瀏覽器開發人員工具“網路”選項卡中,當我嘗試檢查輸出時顯示此錯誤。
<div id="error"><p class="wpdberror"><strong>WordPress database error:</strong> [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 ''pr4_recipe_ratings'
where recipe_id ='104' and user_ip='::1'' at line 2]<br /><code>SELECT COUNT(*)
from 'pr4_recipe_ratings'
where recipe_id ='104' and user_ip='::1'</code></p></div>{"status":2}
uj5u.com熱心網友回復:
您可能會嘗試洗掉'包含您的表名的 ,因為資料庫引擎可以理解您SELECT針對字串制作的。
該SELECT陳述句的 mariadb 檔案( https://mariadb.com/kb/en/select/ ) 提到使用 backstick 字符 ` 來參考表名,而不是單引號。
您可以使用反引號參考列名稱。如果您使用表名限定列 >names,請將每個部分分別參考為 >`tbl_name`.`col_name`。
您的查詢變為以下內容
SELECT COUNT(*) from pr4_recipe_ratings where recipe_id ='104' and user_ip='::1'
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/342794.html
標籤:数据库 WordPress的 功能 选择 数数
