See the documentation for $wpdb->prepare()
. You need to pass a String, but you’re typing set @csum
directly in the parameter. Your code should look like this:
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
$balance = $wpdb->query(
$wpdb->prepare(
"SET @csum := (SELECT current_balance FROM exp_ten WHERE tenant_number=%d);
SELECT tenant_number, transaction_amount, (@csum := @csum + transaction_amount) as narrative
FROM exp_tran
ORDER BY tenant_number",
$user->ID
)
);
}
I don’t have any way to test, since I don’t have your database, but that should work.
Also note how I’ve used the second argument of $wpdb->prepare()
to pass the user ID. There’s no point using prepare if you’re not going to pass in variables that way.
Related Posts:
- simple sql query on wp_postmeta very slow
- Using WordPress public query variables
- How to Use Wildcards in $wpdb Queries Using $wpdb->get_results & $wpdb->prepare?
- How many WordPress SQL Queries per page?
- Multipart/formatted MySQL query problem
- How to tell if $query_var isset?
- Are database queries created using WordPress filters protected from SQL injection?
- Grouping related postmeta data via SQL query
- Mysql / WordPress killing my server with 80k users [closed]
- Slow meta_query with about 4 milion record on wp_postmeta
- Select User by Joining Multiple Meta Value Results
- mySQL query. ORDER BY meta_key
- Update slug (URL) of pending posts via phpMyAdmin
- My SQL function to change user_name
- You have an error in your SQL syntax – Help with query
- how would I create a custom query to get all users, and a related post based on a postmeta field?
- Help with MySQL to $WPDB query
- SQL query to select posts from multiple categories
- How do I see the mysql query generated by get_posts( $args )?
- Alter query with posts_clauses to retrieve NULL values last
- MySql Query very slow
- Searching With Apostrophe
- WordPress Query wp-terms SLOW
- Passing parameters to a static front page
- wpdb COALESCE won’t work
- Get posts from category with custom query
- how to insert missing tags into the posts through mySQL?
- Ideas how to search & replace post_content when string contains a newline?
- Valid SQL query return empty
- mysql query – how to escape apostrophe?
- Query the WordPress database to get data together with replaced information
- Optimize slow SQL query for multiple meta values
- Updating with $qpdb->query() always returns 0 rows affected
- Declaring a var, placing it in a query and using the output of the query?
- How to return count of custom post type posts with a specific custom field value via $wpdb?
- WordPress Mysql query and Duplicate
- Help with Related Posts Function
- How to delete all images from code in all post_content
- I want to get on those users their meta value are like “AGENT” .. but this query is not working
- Displaying data from custom table
- Get 2 meta values from meta key column
- PHP Fatal error: Uncaught Error: Call to a member function insert() on null
- SQL query to delete users with multiple meta keys and comments
- OR condition not working
- Trouble migrating custom post types from non-wordpress cms
- Are there any best practices for creating a Like/Favourite feature in WordPress using custom MySQL tables and without any plugins?
- Not getting input from get_query_var
- Performance of wp_get_attachment_image_srcset() and wp_get_attachment_image_url()
- How to make MySQL search queries with quotes
- Custom MySQL Query for Post and Post Meta
- Mysql query and odd results
- sql for querying post and their category
- Access Tables with number prefix
- What is the most efficient way of implementing a favorite post system?
- Why does get_the_time(‘F j’) return November 30 for all posts?
- Query by one meta_key and sort by another (possibly NULL value)
- Query & Sort Comments by custom comment meta
- is_archive() doesn’t work on public query var archive pages?
- How to count get_users query?
- Possible to get posts from multiple meta keys/values in a single query?
- Difference between fragment caching and wp_cache
- How to display all posts with today’s same month and day only?
- Show featured image as background image from postQuery
- Using $wpdb->update but confused on the WHERE in and SET
- Make one query for adding entries to database
- add_query_arg() and empty variables inside
- Display posts with a start OR end date later than current date
- Order posts by custom field DATE value
- Slow queries constantly getting stuck on WordPress database of ~100,000 posts
- Form redirect with query vars to page
- WordPress custom setting page – add flashing message
- Custom query AFTER rss fetch_feed not working
- list all categories that have a certain word in title
- wp_list_pages() refuses to output posts
- Can’t seem to do combined query AND sort?
- How to reset usual $query on search page to push custom $wpdb query there?
- Order by meta value pro first then meta value free in my search function
- How to delete all posts, categories and tags from WordPress database
- $wpdb->prepare affecting the query?
- WordPress SQL search, how to handle SQL Injection?
- Query for Custom Post Type UI Does Not Loop All the Post
- How to manage a particular “order by” for get_search_query()?
- Query to view scheduled or draft post
- Sort a list of a CPT ( job) if a meta is empty
- Restore WordPress Backup Locally?
- Create a new post on a specified publish date via link?
- Exclude Posts from a Widget
- New custom post type entries are not sorted correctly in admin using pre_get_posts
- Query String for the WP_QUERY parameters
- $wpdb query for price in custom field value
- Multiple terms not working on taxonomy
- Sorting of coupons in the right way
- excludeCat function reverses order of blog posts
- Run search query again without pagination gives no results?
- Parsing External Table Arguments
- WP_Query orderby meta key/value suddely stopped working
- Where can I find the SQL to get the most used information by wordpress database?
- WooCommerce Total # orders [closed]
- SQL Query to get post_id from wp_posts and and meta_key(s) from wp_postmeta
- How to figure out correct wp_options to have autoload=’yes’