SQL Quotes failed query - mysql

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.

Related

What is wrong with this MySQL command?

The query that is throwing an error for my MySQL DB is:
SELECT t1.GROUPNAME FROM user_group t0, group t1 WHERE ((t0.users_USERNAME =
?) AND (t1.GROUPNAME = t0.groups_GROUPNAME))
The error info is the following:
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 'group t1 WHERE
((t0.users_USERNAME = 'test') AND (t1.GROUPNAME = t0.groups_GROUP')
Okay so I know the problem is with the group t1 part. But I dont know what is wrong with it.
Click here to see that I have all the needed colums
Can any one find out what the problem could be here?
group is a reserved word in SQL. You should put quotes around it.
Some JPA providers do that automatically, whereas others don't ...

calculate profit 10% from buy price

I have this script calculates profit
Steps :
barangbeli = harsat / diameter
p1 = barangbeli * 10
p2 = prof / 100
result = barangbeli + profit;
thanks
CREATE VIEW tbkeluar as
SELECT mbarang.kdbrg, mbarang.nmbrg, mbarang.spek,if(SUM(bkeluar.qty), SUM(bkeluar.qty), 0)as qty,(tbmasuk.harsat/mbarang.diameter) as hargabeli, ((hargabeli*10)/100 )+hargabeli) as profit
LEFT JOIN bkeluar on mbarang.kdbrg = bkeluar.kdbrg group by mbarang.kdbrg
i have error
#1064 - 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 ') as profit from mbarang
LEFT JOIN bkeluar on mbarang.kdbrg = bkeluar.kdbrg gro' at line 2
The error message you received should be helpful enough to know whats wrong.
1064 - 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 ') as profit from mbarang LEFT JOIN bkeluar on mbarang.kdbrg = bkeluar.kdbrg gro' at line 2
Based from the error message, MySQL says that you have a syntax error because of this character ), with some characters appended for you to locate it: ) as profit from mbarang
As you can see from your SELECT statement, you have an extra )
SELECT mbarang.kdbrg
, mbarang.nmbrg
, mbarang.spek
,if(SUM(bkeluar.qty), SUM(bkeluar.qty), 0)as qty
,(tbmasuk.harsat/mbarang.diameter) as hargabeli
, ((hargabeli*10)/100 )+hargabeli) as profit
^ delete this extra parenthesis
Your SELECT statement (in CREATE VIEW) is missing its FROM clause. Do you mean FROM mbarang?
You're also probably missing at least one more JOIN; the table tbmasuk is mentioned in the SELECT clause, but isn't mentioned anywhere else in the statement.

mysql query seems to be error.can i select the updated column with one query

There seems to be an error in your SQL query. The MySQL server error output below, if there is any, may also help you in diagnosing the problem.
ERROR: Unclosed quote # 173
STR: "
update voucher_nos
set (select voucher_type as points from vouchers where id='1') = points+1
where company_id = '24'
and finance_year='01/01/2014-01/01/2015';
update voucher_nos
set (select voucher_type as points from vouchers where id='1') = points+1
where company_id = '24'
and finance_year='01/01/2014-01/01/2015'";
1064 - 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 '(select voucher_type as points from vouchers
where id='1') = points+1 w' at line 1
helps will be appreciated
and finance_year='01/01/2014-01/01/2015'";
You have an unneccesary " at the end here, which is probably causing the Syntax error.
--
The second problem is that the first part of a SET statement should be a column name, not a value (or a query which returns one). But I'm not sure how to fix that unless you explain a bit more what you're trying to accomplish.

Odd MySQL error on custom WordPress query

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?

MySQL Error "right syntax to use near '/*100,3), '%') AS `Percentage` FROM INFORMATION_SCHEMA.PROFILING"

I got this weird error after trying to execute a query on a large table:
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 '/*100,3),
'%') AS Percentage FROM
INFORMATION_SCHEMA.PROFILING WHERE
QUERY_ID=' at line 1
What does it mean?
EDIT == this is the query
update cities w, states s set w.region_id = s.id
where s.code = w.region and w.country_id = s.country_id
The cities table has around 3 million entries and the states table around 6000
Just for the record I executed this query using a mysql client Navicat.
SQL supports C-style comments:
/* ... */
so it looks like /*100,3 is being interpreted as the beginning of a comment and that comment is wrecking the syntax of the rest of the SQL.