1

Hi im trying to echo single data from a custom table inside my wordpress database. I found the code to print multiple columns. However I just want to print a single field, so my code looks like this:

global $wpdb;
echo $wpdb->get_results( "SELECT * FROM table.column WHERE id = 1" );

Unfortunately it just prints the word 'Array'

1
  • Don't echo out the results of your query. Store them in a variable and then use it later on... Commented Apr 26, 2017 at 6:44

4 Answers 4

1

Try this:

global $wpdb;
echo $wpdb->get_row( "SELECT * FROM table.column WHERE id = 1" );
Sign up to request clarification or add additional context in comments.

1 Comment

echo a row? Did you test this advice? Me thinks not.
1

You are getting an array and trying to echo it.

SELECT a Variable

The get_var function returns a single variable from the database. Though only one variable is returned, the entire result of the query is cached for later use. Returns NULL if no result is found.

<?php $wpdb->get_var( 'query', column_offset, row_offset ); ?> 

SELECT a Row

To retrieve an entire row from a query, use get_row. The function can return the row as an object, an associative array, or as a numerically indexed array. If more than one row is returned by the query, only the specified row is returned by the function, but all rows are cached for later use. Returns NULL if no result is found, consider this when using the returned value in arguments, see example below.

<?php $wpdb->get_row('query', output_type, row_offset); ?> 

check this documentation

Comments

1
global $wpdb;
$result = $wpdb->get_results( "SELECT * FROM table.column WHERE id = 1" );

foreach ($result as $post){
echo $id = $post->columnName1;
echo $id = $post->columnName2;
echo $id = $post->columnName3;
}

or

global $wpdb;
$result = $wpdb->get_results( "SELECT * FROM table.column WHERE id = 1" );

foreach ($result as $post){
$colm1 = $post->columnName1;
$colm2 = $post->columnName2;
$colm3 = $post->columnName3;

echo '<p>'. $colm1 . '</p>'
echo '<p>'. $colm2 . '</p>'
echo '<p>'. $colm3 . '</p>'
}

1 Comment

Welcome to Stack Overflow! Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made.
0
$mylink = $wpdb->get_row( "SELECT * FROM $wpdb->links WHERE link_id = 10" );

OR

$wpdb->get_var( 'query', column_offset, row_offset );

for go there https://codex.wordpress.org/Class_Reference/wpdb

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.