Rails query boolean MySQL error - mysql

Doing any one of these:
notifications.where("read = 'true'")
notifications.where("read = '1'")
notifications.where("read = true")
notifications.where("read = 1")
results in the error:
Mysql2::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 'read = 'true')
However, this works just fine:
notifications.where(:read => true)
Any idea why this could be?

read is mysql reserved keyword you need to use back-ticks around your column name
notifications.where(" `read` = true")
Not familiar with ruby but you can refer to this answer to enclose the column with back-ticks
Mysql Reserved Words

Related

How to replace apostrophe in database via sql

I am trying to replace "?t" string to "'t" (apostrophe t)
I enter this command in phpMyAdmin
UPDATE "myTable"
SET "myColumn" = REPLACE ("myColumn", "\?t", "\'t")
as suggested in How can I use mySQL replace() to replace strings in multiple records?)
I get this 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 #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 UPDATE "myTable"
SET "myColumn" = REPLACE ("myColumn", "\?t", "\'t") at line 1
What is wrong with my command?
Thanks
Don't quote table or column names. If you need to escape them use backticks
UPDATE `myTable`
SET `myColumn` = REPLACE (`myColumn`, '?t', '\'t')

MYSQL where clause causes syntax error

I just cannot find what is wrong with this simple statement:
$stat_qry = mysql_query("SELECT * FROM stats WHERE group=$galgroup") or die("STATS ERROR: ".mysql_error()); $stat = mysql_fetch_array($stat_qry);
i just get: "STATS 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 'group=1' at line 1"
i cannot get it to work with the 'where' clause, if i remove 'where' it works but just lists everything in the database
GROUP is a reserved word so it needs to be between back tick ``
Also, if $galgroup can be a string and not only a number, you need to add quotes arround it :
$stat_qry = mysql_query("SELECT * FROM stats WHERE `group`='$galgroup'") or
die("STATS ERROR: ".mysql_error()); $stat = mysql_fetch_array($stat_qry);

Warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL

Warning: 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 'color:red'>
and this my code :
$db->query("UPDATE members set id='{$this->test['id']}',
lvl='{$this->userlvl}', ip='{$this->test['IP']}',
time='{$this->test['time']}',
linechat='{$this->test['msg']}'
WHERE user='{$this->test['name']}'");
I'm a beginner so please tell me what is must be ^^
I have tried this
$fixchat = mysql_real_escape_string($this->test['msg']);
$fixname = mysql_real_escape_string($this->test['name']);
$db->query("UPDATE members set id='{$this->test['id']}',
lvl='{$this->userlvl}', ip='{$this->test['IP']}',
time='{$this->test['time']}', linechat='{$fixchat}'
WHERE user='{$fixname}'");
but I got this error :
Warning: 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 ''\wellington\'' at line 1...
One or more of the values in your $this->test array has double quote characters in it. Every dynamic element to a query string must be escaped by passing it through the appropriate escape function (in your case mysql_real_escape_string()). That will escape the quotes so the string is interpreted correctly.
Side note: You should be using the mysqli PHP library instead of mysql, which is deprecated. Also, a better alternative solution is to use parameterized queries.
solved by : addslashes function
$fixchat = addslashes($this->test['msg']);
$fixname = addslashes($this->test['name']);
$db->query("UPDATE members set id='{$this->test['id']}',
lvl='{$this->userlvl}', ip='{$this->test['IP']}',
time='{$this->test['time']}', linechat='{$fixchat}'
WHERE user='{$fixname}'");

MySQL Syntax with DELETE FROM?

I have this bit of SQL that always returns an error, though I can't find why it is returning the error. I have connected to the database with no errors. I'm running PHP 5.2.17, MySQL 5.5.25a, and Apache 2.4.2.
The SQL:
DELETE FROM mail WHERE to=1
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 'to=1' at line 1
TO is a reserved word, you need to use backticks:
DELETE FROM mail WHERE `to` = 1
By adding backticks on the column name escapes in from MySQL Reserved Word
DELETE FROM mail WHERE `to`=1
if the column to is not e.g. INT or DEC you should make it to = "1"

What is wrong with this SQL query? A nonsensical error is occurring.

I'm using the following SQL query:
SELECT * FROM Articles WHERE Name = 'Name' AND Column = 'Column'
And I'm receiving the following 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 'Column = 'Column' LIMIT 0, 30' at line 1
I see no syntax error whatsoever. What is the problem?
Column is a reserved word so you need to encapsulate it between ` chars:
AND `Column` = 'Column'
It's a bad idea to have a column named column, which is a reserved word. Can you change it in your table? Rather than having to work around the design in every query that references it, you're better off fixing it if you can.