0

I am new to WordPress and I am trying to retrieve data from the database using wpdb.

I have created a custom template in order to add the PHP code but when I tried to display the retrieved data, it did not show anything.

where is the error ?

search info.php

 <?php
/*
Template Name: search info
*/

get_header();

global $wpdb;  
$result = $wpdb->get_var('select owner-name from owner-info where owner-id= 5');
echo $result;

get_footer();
?>
3
  • You are looking for blog author name? Clarify your requirement. Commented Feb 8, 2017 at 12:36
  • no its a custom table that i created and contain these required fields Commented Feb 8, 2017 at 12:39
  • You should use delimited identifier like backticks for mysql Commented Feb 8, 2017 at 12:47

4 Answers 4

1

In wordpress, there is a table prefix (default : 'wp_' or which you have provided) in each table name, please check & include in the table name if required, then your table name will be 'wp_owner-info' & if 'wp_' is not added in your table name then keep as it is.

And the code will be:

With prefix i.e. table_name = wp_owner-info

<?php
/*
Template Name: search info
*/

get_header();

global $wpdb;  
$result = $wpdb->get_results("select owner-name from wp_owner-info where owner-id= 5");
echo $result;

get_footer();
?>

OR

Now without prefix i.e. table_name = 'owner-info'

<?php
/*
Template Name: search info
*/

get_header();

global $wpdb;  
$result = $wpdb->get_results("select owner-name from owner-info where owner-id= 5");
echo $result;

get_footer();
?>

I hope, this will be useful for you.

Sign up to request clarification or add additional context in comments.

2 Comments

table name is owner-info you are saying that i need to rename the table name to wp_owner-info ?? or what ?
If you are saying that your table_name is owner-info, then use the code of without table prefix. Find my updated code.
0

Try to use custom query with global varibale $wpdb where you can use dynamic database prefix of table name in query as standred method

$tablename = $wpdb->owner-name;
$ownerID = 5;
$query = $wpdb->prepare( "SELECT * FROM $tablename WHERE owner-id=%s",$ownerID );
$result = $wpdb->get_results( $query );

5 Comments

i tried your answer it display the word "array" still not working
yes, then try var_dump($result); or print_r($result); and check what do you get.
make sure about your table name from which your trying to fetch data.
table name is owner-info it is right but still the same result after using var_dump and print_r
remove $wpdb-> and use $tablename = 'owner-name'; in above code.
0

I don't know about your table structure, but dash - is not a valid character in table or column names. Replace it with the appropriate char, most likely an underscore _, so it becomes

$result = $wpdb->get_var('select owner_name from owner_info where owner_id= 5');

If the name really includes -, you must quote identifiers with backticks, see MySQL - Schema Object Names for details

$result = $wpdb->get_var('select `owner-name` from `owner-info` where `owner-id`= 5');

Comments

0

If its showing blank or white page. then you need to enable wordpress debug mode to know what's wrong with your code. Enable WP Debug

  1. Open wp_config.php file under WordPress root directory.
  2. Find define( 'WP_DEBUG', false ); and set it to true. example define( 'WP_DEBUG', true );

Refresh your WordPress page. here you got the error message. Post your error message here. We will give you the fix. you can fix it by yourself :)

1 Comment

thanks for the advice i didn't know about it ... bust still the same

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.