Wordpress Wpdb->get_results()

 In this Blog We will going to learn everything  about get_results function of wordpress.

wpdb->get_results output type:

get_results(string $query = null, string $object = OBJECT )

get_results takes two arguments as parameter. query and object.

  • query:  it is the sql query you want to run for fetching the results from the database
  • object: it can take four values ARRAY_A | ARRAY_N | OBJECT | OBJECT_K. You can pass any of these. default value is OBJECT.  it returns the integers index based array of row objects. you can use -> to access each column field from single row object

This Wordpress method returns a returns a complete results set from the database according to the query you are executing from the result set you can extract any specific column value.

function show_job_list(){
	global $wpdb;
	$tablename=$wpdb->prefix.'posts';
	ob_start();
	$posts= $wpdb->get_results( $wpdb->prepare("SELECT job_title FROM $tablename"));
	?>

    <?php
	foreach( $posts as $post) {?>
                <div class='show-interview-title'>
		   <article><h1>
			<?php echo $post->job_title; 
			  
			   ?>
			</h1></article>
                </div>
    <?php
	}
	return ob_get_clean();
}

Above function can be written in functions.php. In above example get_results() method is returning all the data from column job_title from table wp_posts. wpdb->prefix is the default wordpress DB prefix which is mentioned in config file. you can update this prefix value as well. In this example Database query is only extracting job_title column but you can also mention * to getch all the columns value from the table.

Accessing specific column value from get_results output:

In above example we have the column job_title and we are trying to fetch all the value from the column job_title.

function show_job_list(){
	global $wpdb;
	$tablename=$wpdb->prefix.'posts';
	$posts= $wpdb->get_results( $wpdb->prepare("SELECT job_title FROM $tablename"));
	
	foreach( $posts as $post) {
		echo $post->job_title; }}

In above example we are fetching all the data from column job_title using wordpress get_results. There can be more than one row in the get_results so you can use foreach loop to go through every row from the database. for fetching value from any specific column use (->column name).In above example we are trying to fetch column having title job_title so we have used $post->job_title.

Wordpress get_results() returning empty results.

This issue generally happens if there is any invalid sql synatx in the query. So there are few basic steps which you can use to debug such issue.

  • use wpdb->show_errors() use this method incase you want to debug empty results issue. But sometime what happen is the show_errors() don't show any error message. In that case you can use wpdb->last_query and check for the syntax of the query last executed.
  • use wpdb->last_query in you php code and try to echo it. This will print the last query which got executed in you php code. If you are unable to find the issue with the query than in that case you can generally use online query syntax checker which can help a lot. 
  • Also for sql query having 'WHERE' clause user sometimes don't pass the correct value.User directly pass value stored in some php variable but that value does not contains the single quote around the values. for example of wrong query "SELECT job_title FROM $tablename where job_name=$job_name"
in the above query user is using $job_name variable which is not surrounded by single qoutes.

Corrected Query will be 

"SELECT job_title FROM $tablename where job_name="."'".$job_name."'"


Hope this article will be helpful

how-to-add-php-code-in-wordpress-page

custom submit form in wordpress

Comments

Popular posts from this blog

How to Create Custom form in Wordpress without plugin

How to add php code in wordpress page