简体   繁体   中英

Retrieve data from Wordpress custom table using PHP

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'

Try this:

global $wpdb;
echo $wpdb->get_row( "SELECT * FROM table.column WHERE id = 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

$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

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>'
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM