Skip to content
Advertisement

SQL SELECT query to get stock qty does not return value in function ( but does from phpMyAdmin)

I am running this code in a function in WordPress/Woocommerce to update stock values to the wp_stock_log table when I manually edit stock levels. I am attempting to get the most recent ‘qty’ value for the product and then set it as var $old_qty. I then want to insert it into the table in a new row as ‘old_qty’ so I can see how much the stock level has increased by.

When I run the SELECT query in phpMyAdmin I am successfully getting the last row’s ‘qty’ but when I run the function in wordpress I always get a value of 1.

I have tested to see if the value being returned from the sql query is an integer and it isn’t – but not sure why as the ‘qty’ column is type integer? Even when I convert it to an integer I always get $old_qty value as ‘1’.

Why am I always getting a value of 1?

EDIT The wp_stock_logs table structure is as follows:

Advertisement

Answer

As I am only wanting to retrieve a single value from the database I should use get_var and not get_results (which returns an array).

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement