doulao2916
2015-10-07 13:40
浏览 50
已采纳

Wordpress:从数据库中检索值

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

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

and I was't able to find my mistake.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongling2038 2015-10-07 13:43
    已采纳

    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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题