最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Passing Current User ID into SQL statement

programmeradmin0浏览0评论

I have the following code:

$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]";

How can I correctly pass the current user ID into the WHERE clause?

I have the following code:

$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]";

How can I correctly pass the current user ID into the WHERE clause?

Share Improve this question asked Jun 8, 2020 at 14:29 sw123456sw123456 1318 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

No need to use []... just use simple single quotations (also no quotations does the job):

$sql ="
    SELECT 
    ...
    WHERE wp_users.`ID` = '$userID'";

OR

$sql ="
    SELECT 
    ...
    WHERE wp_users.`ID` = $userID";
发布评论

评论列表(0)

  1. 暂无评论