Support » Developing with WordPress » Queries written in theme pages doesn’t return the result.

  • pradeep101

    (@pradeep101)


    I wrote the following code in front-page.php

    $current_user = wp_get_current_user();
    echo $current_user->ID;
    echo $current_user->user_login;
    
    global $wpdb;
    $wp_usersinfo = $wpdb->get_row(
        $wpdb->prepare(
            "SELECT * from wp_users WHERE name = %s",$current_user->user_login
        ),ARRAY_A
        );
    
    print_r($wp_usersinfo);

    Which doesn’t print any result of the queries but result of echo $current_user->ID and echo $current_user->user_login are printed. Where as same output was given when i tested in localhost.(currently i am coding to host in a domain)
    But when i run code like:

    print_r(count($wpdb->get_var('SHOW TABLES LIKE "wp_users"')));

    Output is generated.
    Even when i try to make a function in functions.php and try to echo in front-page.php its doesn’t work.
    What am i doing wrong? Please help. Thank you

    • This topic was modified 3 weeks ago by pradeep101.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Use user_login instead of name.

    Why wouldn’t you use get_userdata() instead? What you have, once corrected, is fine. Just curious.

    Thread Starter pradeep101

    (@pradeep101)

    Thanks for pointing it out. But the real problem is. I created a custom table “userrole” of these attribute:
    id int(11) NOT NULL AUTO_INCREMENT,
    name varchar(255) NOT NULL,
    role text NOT NULL,
    Phone int(11) NOT NULL,
    Address varchar(255) NOT NULL,
    profilepic varchar(255) NOT NULL,
    verify tinyint(1) NOT NULL,
    vkey varchar(255) NOT NULL,
    temp_pass varchar(255) NOT NULL,
    email varchar(255) NOT NULL,
    firstName varchar(255) NOT NULL,
    lastName varchar(255) NOT NULL,
    PRIMARY KEY (id)

    And tried:

    $userrole = $wpdb->get_row(
        $wpdb->prepare(
            "SELECT * from userrole WHERE name = %s",$current_user->user_login
        ),ARRAY_A
        );
    print_r($userrole);

    where name of the userrole matches with wp_users->user_login. And the result is not shown. Which was not problem when i was doing it in localhost. Could you help me? Thank You

    Moderator bcworkz

    (@bcworkz)

    Check the PHP error log for any possible clues. WP logs database errors there. Or get into the phpMyAdmin app and try the query there. If there are any issues, more specific information may be available.

    One possible reason is the user that WP uses for DB access doesn’t have adequate permissions for that specific table. Be sure you log into phpMyAdmin using the credentials in wp-config.php.

    When in phpMyAdmin, verify that the value used for $current_user->user_login really does exist in the table.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.