Reputation: 85
I would like this function in my plugin to return data when it's called for. I'm just trying to get any result back at this point. It used to be a more specific query.
function ml_results() {
global $wpdb;
$results = $wpdb->get_results( 'SELECT * FROM wp_ml_char', OBJECT );
echo "<p>Your character's info {$results}</p>";
}
add_shortcode('ml_return', 'ml_results');
I've had no luck returning and displaying ANYTHING with any output_type. I've also tried print instead of echo. I've also tested my query in phpmyadmin which works just fine. All I get returned is "ARRAY". So, $results returns "Your character's info ARRAY". Var_dump also works just fine, but that's not specifically how I would like my data displayed. So I belive the problem lies in the Wordpress code, but I'm just not sure what part of the code exactly.
Upvotes: 0
Views: 299
Reputation: 3757
Your results are returned in array, that is why, they can't be printed out as a string. Actually, you have set flag OBJECT, and results are being returned as object, containing field values for all records in wp_ml_char table. You should try something like this:
function ml_results() {
global $wpdb;
$results = $wpdb->get_results( 'SELECT * FROM wp_ml_char', OBJECT );
$output = '';
foreach($results as $item){
//and then for each field in wp_ml_char table you want to set in output should append to $output var:
$output.= $item->field_name . " "; // field_name should be changed to name of actual field in wp_ml_char table
}
echo "<p>Your character's info {$output}</p>";
}
add_shortcode('ml_return', 'ml_results');
One other thing that bothers me is actual MySQL statement, where you haven't defined more precise condition, and this query you have now as it is, would return all results from wp_ml_char table, so you should add some condition also and probably some limit and offset.
Upvotes: 1