当我尝试使用 SELECT COUNT(*) 函数来计算 table 有条件的行时,显示语法错误?
Showing me a syntax error while I am trying to using the SELECT COUNT(*) function to count table rows with conditions?
我正在使用这个代码>>>
$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}
您可能会尝试删除包含您的表名的 '
,因为数据库引擎可以理解您针对字符串创建 SELECT
。
SELECT
语句 (https://mariadb.com/kb/en/select/) 的 mariadb 文档提到使用反字符 ` 来引用表名,而不是单引号。
You can quote column names using backticks. If you are qualifying column >names with table names, quote each part separately as >`tbl_name`.`col_name`.
您的查询如下
SELECT COUNT(*) from pr4_recipe_ratings where recipe_id ='104' and user_ip='::1'
我正在使用这个代码>>>
$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}
您可能会尝试删除包含您的表名的 '
,因为数据库引擎可以理解您针对字符串创建 SELECT
。
SELECT
语句 (https://mariadb.com/kb/en/select/) 的 mariadb 文档提到使用反字符 ` 来引用表名,而不是单引号。
You can quote column names using backticks. If you are qualifying column >names with table names, quote each part separately as >`tbl_name`.`col_name`.
您的查询如下
SELECT COUNT(*) from pr4_recipe_ratings where recipe_id ='104' and user_ip='::1'