PHP-WordPress:从数据库中检索值

我正在尝试使用此查询从数据库检索字段的文本值: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_...

我正在尝试使用此查询从数据库检索字段的文本值:

<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; ?>" 

我收到的是:

我已经搜索了很多,但我只能找到Class Reference/wpdb

而且我找不到我的错误.

解决方法:

使用$wpdb-> get_var(‘your query’)代替$wpdb-> get_results()

我在查询中发现错误,即您忘记了从wordpress预定义表中检索信息所需的表前缀.

将全局$table_prefix与$wpdb一起使用
像这样:全局$wpdb,$table_prefix

还要确保将您的列last_link添加到tableprefix_users的表中
根据您的要求,使用以下代码检索信息.

<?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;?>">

如文档中所述

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.

本文标题为:PHP-WordPress:从数据库中检索值

基础教程推荐