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;
?>
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:
column_name
to proper column name you want to get.plugin_table
to proper table name you want to get the value from.ID
to proper name of column that stores the user id in given table.