我有以下代码:
$userID = get_current_user_id();
echo $userID;
$sql ="
SELECT wp_users.`user_email`,
wp_wp_pro_quiz_statistic.`correct_count`,
wp_wp_pro_quiz_statistic.`incorrect_count`,
wp_wp_pro_quiz_category.`category_name`
FROM wp_users
INNER JOIN wp_wp_pro_quiz_statistic_ref
ON wp_users.`ID` = wp_wp_pro_quiz_statistic_ref.`user_id`
INNER JOIN wp_wp_pro_quiz_statistic
ON wp_wp_pro_quiz_statistic_ref.`statistic_ref_id` = wp_wp_pro_quiz_statistic.`statistic_ref_id`
INNER JOIN wp_wp_pro_quiz_question
ON wp_wp_pro_quiz_statistic.`question_id` = wp_wp_pro_quiz_question.`id`
INNER JOIN wp_wp_pro_quiz_category
ON wp_wp_pro_quiz_question.`category_id` = wp_wp_pro_quiz_category.`category_id`
WHERE wp_users.`ID` = $[userID]";
如何将当前用户ID正确地传递到WHERE子句中?
最合适的回答,由SO网友:Mohsen Haeri 整理而成
无需使用[]。。。只需使用简单的单引号(也可以不使用引号):
$sql ="
SELECT
...
WHERE wp_users.`ID` = \'$userID\'";
或
$sql ="
SELECT
...
WHERE wp_users.`ID` = $userID";