$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - WPDB: how to get the value of a field in a custom database table|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - WPDB: how to get the value of a field in a custom database table

matteradmin9PV0评论

For my WordPress site, I would like to echo the value of a column in a row (determined by the user's ID) of a table in the database for my site. This table was created by a plugin so it is not accessible through the user meta. I have to try and get the value from the table created by the plugin.

For this example, the value is located in the "plugintable" table in the database and the column is "columnname". How get I get the value based on matching the user's ID with the "ID" row?

I've gotten here from the help of other online sources, but I'm stuck.

<?php
    global $wpdb, $table_prefix;
    $user_ID = get_current_user_id();
    $value = $wpdb->get_var('SELECT columnname FROM '.$table_prefix.'plugintable WHERE ID = '.$user_ID);

    echo $value;
?>

For my WordPress site, I would like to echo the value of a column in a row (determined by the user's ID) of a table in the database for my site. This table was created by a plugin so it is not accessible through the user meta. I have to try and get the value from the table created by the plugin.

For this example, the value is located in the "plugintable" table in the database and the column is "columnname". How get I get the value based on matching the user's ID with the "ID" row?

I've gotten here from the help of other online sources, but I'm stuck.

<?php
    global $wpdb, $table_prefix;
    $user_ID = get_current_user_id();
    $value = $wpdb->get_var('SELECT columnname FROM '.$table_prefix.'plugintable WHERE ID = '.$user_ID);

    echo $value;
?>
Share Improve this question edited Jan 31, 2019 at 22:34 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Jan 31, 2019 at 19:13 StevenSteven 231 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 6

OK, so first of all, you should always use proper escaping, when building the queries.

This is how your code can look after fixing:

global $wpdb;
$value = $wpdb->get_var( $wpdb->prepare(
    " SELECT column_name FROM {$wpdb->prefix}plugin_table WHERE ID = %d ",
    get_current_user_id()
) );

The things you have to take care of:

  1. You have to change column_name to proper column name you want to get.
  2. You have to change plugin_table to proper table name you want to get the value from.
  3. You have to change ID to proper name of column that stores the user id in given table.
Post a comment

comment list (0)

  1. No comments so far