Support » Developing with WordPress » Mystical wpdb query problem. What I’m doing wrong?

  • I can’t understand what I’m doing wrong. The first situation returns NULL. The second returns correct value. Why the first is not working? (Only the first line is different). The user is logged in and I get the correct id with echo.

    $user_ID = get_current_user_id();
    echo ($user_ID); // 2
    $info = $wpdb->get_var( "SELECT info FROM info_table WHERE user_ID = $user_ID" );
    var_dump($info); // NULL
    $user_ID = 2;
    echo ($user_ID); // 2
    $info = $wpdb->get_var( "SELECT info FROM info_table WHERE user_ID = $user_ID" );
    var_dump($info); // string(1) "1"
    • This topic was modified 5 years, 3 months ago by strategeek.
Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in, in order this to work. Are you logged in?

    And when you’re logged in, but INIT is not called, it will give you a NULL.

    Try…

    add_action(‘init’, ‘myFunction’);

    function myFunction(){

    $user_ID= get_current_user_id();

    }

    And then do an if and else statement. If user_id != NULL than ….
    Else you echo out that he still can not get the user_id….

    Thread Starter strategeek

    (@strategeek)

    Hi Djenci. Yes, I’m logged in.

    $user_ID = get_current_user_id();
    echo ($user_ID);

    When I do echo, I get the correct value which is 2

    Thread Starter strategeek

    (@strategeek)

    Hah, I finally found a bug. It was a problem with IDs…

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Mystical wpdb query problem. What I’m doing wrong?’ is closed to new replies.