Complex Database Operations in WordPress

If you want to retrieve an entire table row, then the get_row() function is use for this purpose. This get_row() function by default return the row data as an object, a numerically indexed array, and it can be an associative array. I write below an example see it:

Displaly the post title of post title using query in WordPress
Displaly the post title of post title using query in WordPress

Above example displaly the post title of post ID 7. $thepost object are the column names from the table you queried from database, here is wp_posts in this case. You can send in an additional parameter to the get_row() function forretrieve the results as an array:

Using get_row function data return as an array.
Using get_row function data return as an array.

Your post data return as an array by using the ARRAY_A parameter in get_row(). And get post data as numerically indexed array then you could use the ARRAY_N parameter. The get_results() function for retrieving multiple rows of data from the database When use the standard SELECT queries should use. Following function returns the SQL result data as an array:

Querying the all published post and then displaying the post titles only in WordPress
Querying the all published post and then displaying the post titles only in WordPress

Above example querying the all published post and then displaying the post titles only. Query results are stored as an array in $ publishedposts variable, By the loop which you can display your query values.

For UPDATE and INSERT statements the WordPress database class also features specific functions. For custom SQL queries these two functions eliminate the need because the WordPress will create them for you based on the values passed into the function. The insert() function here is how structured:

$wpdb->insert( $table, $data );

In this function $table variable use as the table name where you want to insert the data. Variable $data isan array of field names and data to be inserted into those field names. If you wantto insert data into a custom table, you would run this:

Insert data in WordPress using custom query.
Insert data in WordPress using custom query.

Above example first thing you do that is set two variables to store the data you want to insert in the database. Then you execute the insert() function for insert the data, then passing these both variables through an array. Have you noticed that how you set field_one and field_two as the two fields you are inserting in the table. So you can pass any field available in the table when you are inserting with data to insert into that field. Another function update() it works very similarly to the insert() function, It need Where clause also variable so WordPress knows which records to update in the table:

$wpdb->update( $table, $data, $where );

Here you see the three parameters in this function so in this function the $where variable is an array of field names and data for the SQL WHERE clause. In the $where variable normally set to the unique ID of the field you are updating, I may be also contain other field names from the table.

Update data in WordPress using custom query.
Update data in WordPress using custom query.

In the above example first you set your updated title then content variables. Must set the variable $my_id, which contains the ID of the post you want to update. Next, you execute the update() function. Have you notice that the third parameter of the update function you send is an array containing your WHERE clause values this is the post id. Above query is updating the title and content for post ID 7. In the WHERE parameter when updating a table record you can send also more than one parameter. Do not need to be wrapped with the prepare() function of insert() and update() functions. These both functions actually use the prepare() function after concatenating the query from the values passed to the functions. If manually creating your INSERT and UPDATE queries in WordPress this is a much easier method.