I would like to get rows from another database so I created query:
SELECT * FROM database-test.users
MySQL result that error:
Database_Exception [ 42000 ]: SQLSTATE[42000]: Syntax error or access violation: 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 '-test.users' at line 1
How to solve this?
Thanks for reply
You need to do it like below (use back-ticks around table name):-
SELECT * FROM `database-test`.users
Or
SELECT * FROM `database-test`.`users`
I would recommend to 'back tick' all database and table names in your query. It will tell the database's SQL parser to ignore any special characters such as "-" and consider them as part of the name.
Example:
SELECT * FROM `database-test`.`users`
Try
SELECT * FROM `database-test`.users
As you can see, I have used the ` character to encapsulate database-test, which makes sure that non alpha-numeric characters, like - will be accepted in the name.
Related
I am using MySQL 5.6 , one of my table field contains JSON data. I am getting syntax error when using below query -
SELECT * FROM products WHERE device_id = '1212'and product_id = '54'and option = '"{"229":"20"}"'
field option has value as {"229":"20"} I am getting following 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 'option = '"{"229":"20"}"'
LIMIT 0, 25' at line 1
Please suggest any solution , thanks much!!!
option is a reserved keyword in MySQL. Use backticks to escape it or choose a different name
... `option` = ...
I'm getting this error:
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'AS `Colleges__*` FROM college_admins CollegeAdmins LEFT JOIN colleges Colleges O' at line 1
Here is the SQL query which is giving this error:
SELECT Colleges.* AS `Colleges__*` FROM college_admins CollegeAdmins LEFT JOIN colleges Colleges ON Colleges.id = (CollegeAdmins.college_id) WHERE CollegeAdmins.user_id = :c0 LIMIT 20 OFFSET 0
I enabled quoteIdentifiers config\app, but it leads to this new error:
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'AS `Colleges__*` FROM `college_admins` `CollegeAdmins` LEFT JOIN `colleges` `Col' at line 1
where the query becomes:
SELECT `Colleges`.* AS `Colleges__*` FROM `college_admins` `CollegeAdmins` LEFT JOIN `colleges` `Colleges` ON `Colleges`.`id` = (`CollegeAdmins`.`college_id`) WHERE `CollegeAdmins`.`user_id` = :c0 LIMIT 20 OFFSET 0
I think it's taking the 'Col from Colleges as the keyword 'COL', but I'm not sure. How to fix this?
This is the CakePHP code which is generating the MySQL query:
return $college_admins->find()
->select(['Colleges.*'])
->leftJoinWith('Colleges')
->where(['CollegeAdmins.user_id' => $userId]);
You cannot use Colleges.* in a CakePHP ORM query (CakePHP 3.x). As you've discovered this creates incorrect SQL aliases like Colleges__*. Instead to select all columns of a table you need to pass a table object.
So you'd probably be wanting to do something like:-
->select($college_admins->Colleges)
Assuming Colleges is associated with your CollegeAdmins table.
You cannot alias colleges.*, since this refers to all columns within colleges table and aliases refer to a single column (or table or subquery). You need to list all fields within the colleges table and provide an alias for each of them, such as
select colleges.ig as colleges_id, colleges.field1 as colleges_field1, ...
There is not syntax in sql to provide alias such way. What you may try to do is to access the metadata returned by mysql in php to retrieve the table name for each field.
I'm getting MySQL error 42000:1064 that suggests a general syntax error with the following SQL:
UPDATE `events` SET ?=?, ?=?, ?=now() WHERE `event_id`=?;
PHP code to convert to a readable statement & also execute:
<?php
$ar = array_fill(0,count($args),'/\?/');
echo preg_replace($ar,$args,$sql,1);
$this->execute($sql, $args);
?>
This evaluates to:
UPDATE `events` SET event_name=test, form_id=webform, last_updated=now() WHERE `event_id`=124;
Which when pasted into the MySQL workbench completes successfully.
[mysqlErrorMsg] => SQLSTATE[42000]: Syntax error or access violation: 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 ''event_name'='test', 'form_id'='webform', 'last_updated'=now() WHERE `event_id`=' at line 1
It should be noted that my user has full access to the table in question.
You can't use placeholders on column names. Only on values.
Your query does NOT evaluate to (as it should)
UPDATE `events` SET event_name=test, form_id=webform, last_updated=now()
WHERE `event_id`=124;
but is being evaluated as this instead:
UPDATE `events` SET 'event_name'='test', 'form_id'='webform', 'last_updated'=now()
WHERE `event_id`=124;
See the quotes? These are strings, not column names.
So hard code the column names and only use placeholders for values
UPDATE `events` SET event_name=?, form_id=?, last_updated=now() WHERE `event_id`=?;
I'm am working on a project and need to use the below query statement, unfortunately my table and column names have dashes. Does anyone know how to get this to work?
SELECT * FROM 'default-table' WHERE 'ds-avail'='Yes';
Here is the error I get.
Invalid query: 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 * FROM [default-table] WHERE [ds-avail]='Yes'' at line
Try this
SELECT * FROM `default-table` WHERE `ds-avail` = 'Yes';
You used 'table' while you should have it like this table
SELECT * FROM default-table WHERE ds-avail='Yes';
Be sure your table is named exactly "default-table" (without quotes)
Be sure the field that you're looking for is called "ds-avail" (without quotes) and it exists in that table.
You should put the table and the column without quotes:
SELECT * FROM default-table WHERE ds-avail='Yes';
I got this error :
Database problem occur, please try again later.
- Error in query: INSERT INTO main SET title ='', url='www.jerseymurah.com', kod='jerseymurah', owner='Hasbul Aqill', tag='jersey, football, world cup', since='Feb 2010', desc='ssfsfsfsfs'
- 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 'desc='ssfsfsfsfs'' at line 1 (Error #1064)
- File: /home/yosh/domains/yosh.my/public_html/demo/admincp/tambah-save.php
and this is my mysql query code :
$query = "INSERT INTO main SET title ='".$ttile."', url='".$url."',
kod='".$kod."', owner='".$owner."', tag='".$tag."', since='".$since."',
desc='".$desc."'";
$db->rq($query);
Please help and thanks a lot!
DESC is a reserved word in mySQL.
You need to put that field in backticks:
`desc`="..."
maybe consider renaming the field.
mySQL reserved words in the manual
I think DESC is a reserved word, try escaping it with backticks.
mysql_query("INSERT INTO main(title,url,kod,owner,tag,since,description) VALUES('$title','$url','$kod','$owner','$tag','$since','$desc')");