$wpdb->get_var
will do exactly what the name suggests– return a single value. So this:
$sum = $wpdb->get_var("select sum(first), sum(second) from sums_test where id > 1");
var_dump($sum);
Will return the result of sum(first)
. The rest of the result is lost (though cached I believe).
The method you want is get_row
which will return a complete row of results.
$sum = $wpdb->get_row("select sum(first) as first_sum, sum(second) as second_sum from sums_test where id > 1");
var_dump($sum);
I have added AS
clauses as well to make it easier to use the data in PHP. You can now get your results with $sum->first_sum)
, $sum->second_sum
, and so on for as many as you retrieve, or you may use something something more dynamic like:
foreach ($sum as $k=>$v) {
echo $k.' -> '.$v.'<br>';
}
Related Posts:
- get_results using wpdb
- Export wordpress table to excel
- How to query the WordPress database to get posts of a certain custom post type, taxonomy and field?
- Advanced SELECT query with condtional statements
- How Can I Put Meta_Compare in the Database-Query?
- $wpdb->query can’t insert data
- Custom WordPress Table wpdb
- Best practice to limit results in get_row()?
- Connecting to a different database
- Query Column of Specific ID from Database Table
- How to insert a value to decimal type field using wpdb->prepare?
- Insert NULL value using prepare()
- WordPress Database Query works in phpMyAdmin but not in the code
- Should I Use only wpdb Class to Write Custom Queries?
- Pulling values from a sepcific row in table
- Using wpdb to connect to a separate database
- WPDB Insert or if exists Update
- $wpdb won’t insert NULL into table column
- How to fetch Data in WordPress using MySQLi or $wpdb
- wpdb->insert multiple record at once
- How to define composite keys with dbDelta()
- Does wpdb add considerable overhead on queries with large result sets?
- Hook into $wpdb
- Display data from a non wordpress database on a page template
- How does $wpdb handle COUNT(*)
- WordPress database error: [Query was empty] [closed]
- Connecting to external oracle database
- How best to inject ads between posts in the loop?
- Change SQL get_results to search for posts with custom term in custom taxonomy
- Can’t save ajax value to database
- Updating all rows of table with $wpdb
- How to execute mulitple statement SQL queries using $wpdb->query?
- Is $wpdb->prepare escaping to much? How to use it properly?
- When and why should I use $wpdb different return types?
- How wordpress accesses its own database
- Remove database entries where post_date > expiration date
- Error connecting to DB in /wp-includes/wp-db.php on line 1538 [closed]
- How To Get Some Data From WordPress Database Using WordPress $wpdb Query?
- How to use a different database to list and manage comments in the backend
- Cannot update custom database table row
- wpdb_prepare with multiple or condition
- Show last modified date of database
- $wpdb->insert is not working
- Extracting the post_id via the wp_insert_post action (external db query)
- WordPress database error – Error in SQL syntax – I can’t identify any error?
- In what part of the WordPress core does the users table and usermeta table get joined?
- Any possible way to make $wpdb->get_results() return anything else than array?
- Multiple wp_options tables to share content across installs
- Replace/Mute/Stop Search Query
- Create table from array with prepare
- Update from 4.5 to 4.6 failing
- cache specific data?
- Does WordPress $wpdb functions wait when table is locked?
- How to paginate information obtained from a query to a custom table?
- $wpdb->insert Giving duplicates
- I’m not able to get access to $wpdb [duplicate]
- Have working sql query… trying to adjust it to use $wpdb
- How to fetch records from database WordPress
- Best practice to import user base (subscribers) from one website to another?
- AJAX wp-mysql running too slow
- Problem migrating to localhost
- Clear Terms from Taxonomy for Specific Post IDs?
- SQL query to set posts in bulk based on the post content
- How to delete a particular row in a database table
- How to create more than one new wpdb object?
- Custom search query by category id
- Update multiple rows in one query
- Should I use an additional column in the DB?
- access JSON results from wordpress database with wpdb
- Getting value from database table depending on field value
- wpdb->insert not inserting first variable
- How do I have a user upload a blog post and then retrieve that to display in a card on the site?
- How to escape percentage sign(%) in sql query with $wpdb->prepare?
- How do I get database rows from a custom table using wpdb?
- Does this archive template part look like its making too many calls to the db?
- Join Query on WP_USERMETA Table
- Job and Employee Performance Tracking with Product Safety
- Help with $wpdb on custom code
- “BS_” rows in postmeta table
- Help posting values to DB on submit using $wpdb->query
- How to add download link from database on custom page template?
- wpdb query to insert images in to post/page gallery
- Set SQL_BIG_SELECTS and MAX_JOIN_SIZE on a WP_Query
- 2 $wpdb queries causing error Table ‘wp_postmeta’ is specified twice, both as a target for ‘UPDATE’
- insert data from a form:: Warning Empty query mysqli::query()
- How can I get $wpdb to show MySQL warnings?
- About wp database hooks (error establishing connection)
- Performance bug – slow DB query
- How to get specific table by current user login
- How can we speed up wp_get_nav_menu_items()?
- Accessing content from third party as native posts in WordPress
- Extracted CSV as Array for Custom Query Loop
- Generating an HTML table from an array based on dynamic key values
- Can’t Install WordPress (local) Failed to open file wp-includes/wp-db.php
- using same mysql user with many databases
- Using class id from array for query
- Inserting into data into external DB using WPDB
- Insert Extra fields added in the front end registration form to DB
- $wpdb->insert not working for last select option
- Uploading to WordPress Database