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 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错