You can simplify that by using $wpdb->get_var
.
$total = $wpdb->get_var( "SELECT COUNT( * ) AS total FROM tableA" );
echo "Total Records:" . $total;
However, the code you’ve got should work– either version of it. I tested both.
The only thing I notice is that the first version of the code should give you Array ( [0] => Array ( [total] => 2 ) )
. Where is cnt
coming from?
Related Posts:
- Fetch array with $wpdb
- Create an array with a string key from wpdb->get_results
- How to fetch an array in $wpdb?
- Save data from a checkbox to a wpdb array
- PHP | left join avg query give me Array()
- Access to numerically indexed array in wpdb Class and undefined offset
- Get 3 row ID’s via ARRAY_A
- $wpdb->get_results in not an array
- Echo only gives me the first part of string
- wpdb select from using array as search parameters
- what is the way to see the currently executing query in wordpress?
- WordPress 4.8.1 uses mysql_connect which doesn’t work with PHP 7
- How to use $wpdb to delete in a custom table
- Matching database content types to PHP types
- Does $wpdb->prepare not create a prepared statement that I can execute multiple times?
- Saving custom form data into database
- Get data from database table by post_id to get data from second database table
- wp-content/db.php : where is this file?
- Delete/replace img tags in post content for auto published posts [closed]
- $wpdb sql help. Select post id and post meta value based on 2 other post_meta values
- $wpdb->get_results() returns good result only in first foreach iteration
- $wpdb->insert – inserting multiple rows
- $wpdb prepared with search term
- WPDB->insert with special characters
- wpdb-> not adding prefix to custom table
- Is there a (better) way to access $wpdb results?
- $wpdb->prepare not working with update table prefix
- How to use prepare to query with variables
- Use $wpdb in wordpress theme files
- wpdb->get_row() / get_results – is it possible to return an object instances of my own class?
- Problem in creating table through plugin code
- I want to update my postcontent with $wpdb
- $wpdb->replace / Replace or update primary key
- $wpdb – do you have to use foreach even if there will always only be one result?
- Replicating the WP_Query ‘s’ param with $wpdb
- Need help writing a $wpdb query
- How to use IN array properly in WordPress?
- get the number of queries made on a page(inside my plugin)
- Doing a loop with multiple DBs simultaneoulsy
- $wpdb->insert query changes to SHOW COLUMNS query Output
- Fixing wpdb->get_results and wpdb->prepare?
- How does the WXR file differ from the WPDB?
- WordPress SQL query – returning ‘true’ ‘false’ or ‘null’
- Modify the structure of data returned by $wpdb
- WPDB If primary key already exists, add +1 to integer field
- How to get an array of user roles with or without a specific capability?
- What is wrong with this wpdb update?
- Programmatically Creating Page using $wpdb and getting 404 error
- Return XML of Post Metadata
- Constructing a dynamic WPDB query with multiple LIKEs
- Problem in inserting row to custom database table
- sanitize_text_field and apostrophe problem
- 301 Redirect Code
- wpdb updating record in wordpress with json adds extra array elements
- How to write: $wpdb->update having WHERE NOT value pair in the array
- Counting posts with argument without retrieving the posts
- $wpdb->insert inserting only f character in custom table
- Why does wpdb->update delete other meta?
- Increment integer field in database when WHERE needs to be dynamic [closed]
- Getting variable from Database
- Create new database through static page code
- Custom $wpdb returns unexpected time based results
- WordPress “wpdb->update” – Append Text Value
- Need help converting get_user_meta [keys] into own array
- Can’t call WPDB inside RSS template
- Save sql file after doing insert wpdb
- Can I use wpdb to insert query results into a post?
- $wpdb->prepare error after WordPress update [duplicate]
- $wpdb->get_var next var?
- How do I modify this wpdb query to include posts that have a post_status of publish and draft?
- Query and get meta as object(stdClass) on wp postmeta table?
- How do I query a table?
- check that the data exists before sending it to wpdb
- How to pass an input value into wpdb->Prepare
- no result returns when using $wpdb->get_results with where clause
- SQL queries to another wordpress site
- update not working using wpdb
- get_posts() SQL Injection
- og:image functions.php
- wpdb replace returning 1 where delete and insert is expected
- wpdb discards duplicate column names?
- update_post_meta not working in template_redirect action
- How to work with constraints on wordpress user metadata?
- $wpdb select returns empty array
- Show error messages to a user when database insert fails
- Intermittent problem writing update_user_meta
- How to use WHERE NOT EXISTS query to avoid duplicate entry using $wpdb to save in custom table?
- Combine posts and postmeta
- Getting record from three wpdb tables
- wpdb insert working in one function, but not another
- Fetching array of postmeta with $wpdb and in_array conditional
- WPDB Prepared Delete
- wp_commentdata: error in documentation?
- Can’t get expected result from a wpdb query
- Can’t seem to get set_blog_id working, it just doesn’t reset the blog ID
- DBDelta: “table doesn’t exist” for a table that was just created
- selecting row using wpdb which contain special symbols
- Query Problem in Clustom Plugin
- How does $wpdb->get_var work with offset?
- WP Recommended Table Exclusions?