Understanding $wpdb->get_results With Different Result Formats |
X

Congrats, You are Subscribed to Receive Updates.

Understanding $wpdb->get_results With Different Result Formats


Understanding $wpdb->get_results With Different Result Formats is one of a necessary thing to know while working with  WordPress database results. Here is few examples for you to understand the results and its usage. We can use this awesome function to get results from the query which we need to get datas with filtered results.  Lets take a look the syntax of the get_results function.

<?php $wpdb->get_results( 
	'query', 	//the query string to process and get results with filtered informations. 
	output_type // Here is the next useful thing to set your results format.
); ?>

The above one is the common syntax. The query string every one knows to create and process in it. Its just a Normal  MySQL query string. Lets move the Topic of  Result formats.  The following things are our Result formats,

  • OBJECT
  • OBJECT_K
  • ARRAY_A
  • ARRAY_N

You can get some ideas from the wordpress class reference page. But Here I am going to  show you the differences of the results formats and its usage. Lets discuss each one in detail.

  1. OBJECT : 

The default one, you don’t need to specify the output type. The result will be returned like objects. So you can use it better way. When you need to query some items and if you are familiar with writing of Classes and object, you can play with it well. But its not hard, just look at the following example you will understand. Here i need to query all users login name.

$users = $wpdb->get_results( "SELECT user_login FROM $wpdb->users");
						// The query will get the complete users login id/login name
foreach ( $users as $user ) {
	echo $user->user_login; // Here i used the default object method. So i didnt specify the name here and get all the users name. 
}

 2. OBJECT_K
The method is simple and also similar to the default one. But there is something special in it. It will get the array of objects in the row. But it will use the first column values as the key to the array of the objects. Also, its not much used, because this will be little hard to  confusing while compare with the default and array methods.

3. ARRAY_A

The result will be an array of values. you can get the results by using the column name as the key. The following example help you to understand more.

global $wpdb; 
$users = $wpdb->get_results( "SELECT * FROM $wpdb->users" , ARRAY_A);
foreach ( $users as $user ) {
	echo $user[user_login]. '<br>'; // Here i used the default object method. So i didnt specify the name here and get all the users name. 
}

 The above example get the same user login name, but it works in array basic. “ $user[user_login] ” in place of the ” $user->user_login; ” .

4.ARRAY_N

 The results is some what similar to the ARRAY_A. But there is slight variations in the retrieving method.  The Above method uses the Column name as the array key , but here numerically indexed arrays . Just see the results of the following code.

global $wpdb; 
$users = $wpdb->get_results( "SELECT * FROM $wpdb->users" , ARRAY_N);
						
foreach ( $users as $user ) {
	echo $user[1]. '</h3><br>'; //Here $user[1], 1 is the column number.
}

 While executing the above code you will get all user login names, i mean all the users username, which they use to login. In place of the 1, if you change it to” 0″  and get all the users ID. And similarly, if you change it to 2 you will get all the users password encrypted one.

That’s it. the above things will help you to understand the difference between the results output types.

 

commenter

About Varadharaj V

The founder of Kvcodes, Varadharaj V is an ERP Analyst and a Web developer specializing in WordPress(WP), WP Theme development, WP Plugin development, Frontaccounting(FA), Sales, Purchases, Inventory, Ledgers, Payroll & HRM, CRM, FA Core Customization, PHP and Data Analyst. Database Management Advance Level

2 comments

  1. commenter

    hi Varadharaj,
    nice article …. i need little help with that insert query is working fine but i am trying to delete a database row as well as update my data which is fetching from database
    here is my code


    prefix . "tropix_dmin";
    $query = "
    SELECT post_name1,id
    FROM $post_name_table";

    $post_name_results = $wpdb->get_results($query);

    function delete_row($id){
    global $wpdb;
    $table_name = $wpdb->prefix . 'tropix_dmin';
    $wpdb->query(
    $wpdb->prepare(
    "
    DELETE FROM $table_name
    WHERE id = %d",
    $id
    )
    );
    }

    ?>

    Street Address
    Apartment No
    Detail
    Delete

    post_name1);

    if($this_data != ""){

    ?>

    <a href="/details-list1.php?id=id; ?>" class="thickbox">View Detail

    id . '">delete

Comment Below

Your email address will not be published. Required fields are marked *

*

Current ye@r *

Menu

Sidebar