Odd MySQL error on custom WordPress query - mysql

I'm trying to run a MySQL query via WordPress, to bring back a list of posts that I want to delete because they have no "like" votes (using someone else's plugin data). The query works perfectly in phpMyAdmin but gives a syntax error when I run it through WP... and I see absolutely no reason why it would do this.
Here's the query code, which checks for posts over 30 days old that have no corresponding "like" entry in wti_like_post (whether positive or negative):
$novotesquery = "SELECT * FROM $wpdb->posts
WHERE $wpdb->posts.post_type = 'post'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_date < DATE_SUB(NOW(), INTERVAL 30 DAY)
AND $wpdb->posts.ID NOT IN
(SELECT DISTINCT post_id FROM $wpdb->wti_like_post)" ;
$result = $wpdb->get_results($novotesquery);
The syntax error says there's a problem on the last line of the SQL (the SELECT in parentheses): "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 6".
When I run the query in phpMyAdmin (replacing "$wpdb->" with the table prefix), it works a treat. If anyone can tell me why the SQL query will run on the server and not in WP, I'd appreciate it.
Thanks in advance.

Perhaps it's just a matter of defensive parenthesis
$novotesquery = "SELECT * FROM {$wpdb->posts}
WHERE {$wpdb->posts}.post_type = 'post'
AND {$wpdb->posts}.post_status = 'publish'
AND {$wpdb->posts}.post_date < DATE_SUB(NOW(), INTERVAL 30 DAY)
AND {$wpdb->posts}.ID NOT IN
(SELECT DISTINCT post_id FROM {$wpdb->wti_like_post})" ;
Perhaps you should use $wpdb->query method instead of get_results and finally, perhaps wti_like_posts is not yet declared when your code runs.
What about die($novotesquery) right before "$result" line?

Related

Syntax Error in SQL query. Not able to find mistake

I am writing tests for my database in mysql.
con.query(“SELECT `list`.* FROM `list` WHERE `list`.`place` = 86 AND `list`.`person` = \"#{person_id}\" AND (( (list.state = 'open' AND num=\"#{num}\") || (list.state = ?) )) AND (list.updated_at > \"2018-06-05\") ORDER BY list.person, list.state='closed' DESC, list.updated_at DESC LIMIT 50 OFFSET 1”)
list is a table, and num, place, and person are values of a single item from list. I'm also using this in a ruby script.
I get a syntax error. The error message is:
You have an error in your SQL syntax check the manual that corresponds to your MySQL server version for the right syntax to use near ‘`?) )) AND (list.update_at > “2018-06-05”)) ORDER BY list.person, or`’
I cannot figure out the issue. Please help me.
Look at the ORDER BY part of the query
ORDER BY list.person, list.state='closed' DESC, list.updated_at DESC
list.state should not have an ='closed'
Just try changing the it all to
ORDER BY list.person, list.state DESC, list.updated_at DESC
I think you are using the wrong character for double quotes. You have “. Try " instead.

SQL error when updating menu wordpress

Recently when I tried to make changes to a menu I have in a word press website I received an error message saying
[You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE woocommerce_term_id = 110 AND meta_key = 'thumbnail_id'' at line 1]
SELECT * FROM WHERE woocommerce_term_id = 110 AND meta_key = 'thumbnail_id'
This is the query used
SELECT * FROM WHERE woocommerce_term_id = 110 AND meta_key = 'thumbnail_id'
For now I can still make changes to the site without any problems, but I still think that there'll be a problem when I go live with the site.
Does anyone know a fix for this?
You don't supply a table name in your select query:
SELECT * FROM WHERE woocommerce_term_id = 110 AND meta_key = 'thumbnail_id'
Should be something like:
SELECT * FROM <yourTableName> WHERE woocommerce_term_id = 110 AND meta_key = 'thumbnail_id'
SQL follows the protocol of:
--** pseudocode **
SELECT <tableColumns>
FROM <table or view>
WHERE <your conditions>
For now I can still make changes to the site without any problems, but
I still think that there'll be a problem when I go live with the site.
Most definitely, your query will fail straight away.
Table name is missing next to FROM, pls correctly provide the table name
SELECT * FROM <TABLENAME> WHERE woocommerce_term_id = 110 AND meta_key = 'thumbnail_id';
Please refer for basic mysql commands:
cse.unl.edu/~sscott/ShowFiles/SQL/CheatSheet/SQLCheatSheet.html

Incorrect syntax near the keyword 'select' in web application

I am pretty new to SQL. I used FlySpeed SQL builder to write this query and it works there but when I use this code in web application I get an error:
Incorrect syntax near the keyword 'select'
Basically, I want to write this: If count is below 20 it returns nothing, if it is over 20 it triggers notification system.
Select
sr.status,
sr.assetsiteid
From
sr
Where
sr.status = 'pending' And
sr.assetsiteid = 'gp'
Group By
sr.status, sr.assetsiteid
Having
count(*) > 20
Order By
count(*) Desc

SQL Quotes failed query

I have a weird problem: I get this error;
You have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near 'key = 'aaaa'
AND expire >= '1387730046'' at line 1
You see 2 times ' on the end. But the SQL is;
"SELECT * FROM mails WHERE key = '".$mysql->real_escape_string($_GET['key'])."'
AND expire >= '".strtotime(date("Y-m-d H:i:s"))."'"
So, I don't understand why I get this error. Does somebody know what I doing wrong?
The word key is a reserved word in MySQL (see here). So you need to escape it:
SELECT * FROM mails WHERE `key` = '".$mysql->real_escape_string($_GET['key'])."' AND expire >= '".strtotime(date("Y-m-d H:i:s"))."'"
Query should be:
"SELECT * FROM mails WHERE `key` = '".$mysql->real_escape_string($_GET['key'])."' AND expire >= '".strtotime(date("Y-m-d H:i:s"))."'"
always use parentesis around columns.

MySQL: Syntaxerror when using Join?

Can you find anything wrong with this query?
SELECT * FROM requests
WHERE id = '".$id."'
LEFT JOIN request_data ON (requests.id = request_data.request_id)
GROUP BY requests.id
Been workingon it for a while but can't seem to get it right!
The database looks like this:
-requests
-id
-another column
-and a third one
-request_data
-request_id
-key
-value
EDIT: Oh right, and the error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LEFT JOIN request_data ON (requests.id = request_data.request_id) GROUP BY ' at line 3
Any ideas?
The WHERE is in the wrong place.
SELECT *
FROM requests
LEFT JOIN request_data ON (requests.id = request_data.request_id)
WHERE id = '".$id."'
You probably don't need a GROUP BY either as the WHERE ensures there will only be one id returned unless in some way you are relying on the hidden columns functionality (which you shouldn't as the results are undefined).