WordPress: Retrieve a value from database

I’m trying to retrieve a text value for my field from database using this query:

<input type="text" name="last_link" id="last_link" value="<?php global $wpdb; $user_ID = get_current_user_id(); $result= $wpdb->get_results( 'SELECT last_link FROM users WHERE ID = $user_ID'); echo $result; ?>" 

What I receive is :
enter image description here

Read More

I’ve searched a lot but all I could find is Class Reference/wpdb

and I was’t able to find my mistake.

Related posts

1 comment

  1. USE $wpdb->get_var('your query') instead $wpdb->get_results()

    I have found Error in query which is you forget the table prefix that is required to retrieve info from wordpress pre defined tables.

    Use global $table_prefix along with $wpdb
    Like this : global $wpdb,$table_prefix

    Also make sure your column last_link is added to tableprefix_users‘s table
    For your requirement use below code to retrieve info.

    <?php
    
    global $wpdb,$table_prefix;
    $user_ID = get_current_user_id();
    $last_link = $wpdb->get_var('SELECT last_link FROM '.$table_prefix.'users WHERE ID = '.$user_ID);
    ?>
    
    <input type="text" name="last_link" id="last_link" value="<?php echo $last_link;?>">
    

    As stated in Documentation

    Generic, multiple row results can be pulled from the database with
    get_results. The function returns the entire query result as an array.
    Each element of this array corresponds to one row of the query result
    and, like get_row, can be an object, an associative array, or a
    numbered array. If no matching rows are found, or if there is a
    database error, the return value will be an empty array. If your
    $query string is empty, or you pass an invalid $output_type, NULL will
    be returned.

Comments are closed.