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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?