Есть функция, по вызову которой производится
INSERT INTO `table`(`key1`, `key2`) VALUES('value1', 'value2')
.
Было решено передавать функции ключи и значения аргументами типа «string».
В итоге, получаю:
Fatal error: Uncaught PDOException: SQLSTATE[42000]
Fatal error: Uncaught PDOException: 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 »`name`, `desc`, `queue`, `slug`, `color`, `thumb`, `name__price_1`, `name__pric’ at line 1 in X:domainscm.rucore.php:171 Stack trace: #0 X:domainscm.rucore.php(171): PDOStatement->execute() #1 X:domainscm.ruappsections.php(158): cmSections->createData() #2 {main} thrown in X:domainscm.rucore.php on line 171
Ума не приложу, что там не так.
Строка 171 файла «core.php» с предвещающей ей 170-й:<br />
$query = $qpdo->prepare("INSERT INTO `sections`(:keys) VALUES(:values) LIMIT 1");
return $query->execute(array(':keys' => $keys, ':values' => $values));
$keys
— первый аргумент нижеприведённой функции createData()
, а $values
— второй.
Строка 158 файла «sections.php»:<br />
$sections->createData('`name`, `desc`, `queue`, `slug`, `color`, `thumb`, `name__price_1`, `name__price_2`, `name__prm_1`, `name__prm_2`, `date_create`, `date_update`, `owner`', '' . $_POST['name'] . ', ' . $_POST['desc'] .', ' . $_POST['queue'] . ', ' . $_POST['slug'] . ', ' . $_POST['slug'] . ', ' . $_POST['color'] . ', ' . $_POST['thumb'] . ', ' . $_POST['name__price_1'] . ', ' . $_POST['name__price_2'] . ', ' . $_POST['name__prm_1'] . ', ' . $_POST['name__prm_2'] . ', ' . date('Y-m-d h:i:s') .', ' . date('Y-m-d h:i:s') . ', ' . $_COOKIE['user__id'] . '');
Oops!! Stuck with MySQL Error code 1064 SQL State 42000? We can help you in fixing it.
The SQL State Error 42000 occurs mainly due to the SQL syntax error or due to the outdated JDBC MySQL driver.
At Bobcares, we often get requests to fix MySQL errors, as a part of our Server Management Services.
Today, let’s see how our Support Engineers fix MySQL errors for our customers.
Why MySQL Error code 1064 SQL State 42000 occurs?
The MySQL Error code mainly occurs due to the SQL Syntax error. It happens when MySQL is unable to validate the commands.
The Syntax Error occurs due to many factors like mistyping the commands, deprecated or missing data from the database.
In some cases, the error occurs when the JDBC driver initializes the connection.
How we fix the MySQL Error code 1064?
Recently, one of our customers approached us saying that he is getting MySQL Error code 1064 SQL State 42000. On checking, we found an error in the SQL syntax.
Now, let’s see the main causes for this Error 1064 SQL State 42000 and how our Support Engineers fix them.
1. Using Reserved Words
The reserved words perform some specific functions within the MySQL engine.
Sometimes we receive the error while using the reserved words, The error occurs when the MySQL is not meeting the exact requirements for using the particular keyword.
Create Table alter (first name, last name);
The alter is a reserved word. To fix the error 1064 with the reserved word we specify the alter word within backticks.
Create Table 'alter' (first name, last name);
2. Outdated JDBC driver
When the JDBC driver initializes the connection, it sends several commands to the MySQL server. At that time we may receive the MySQL Error code SQL State 42000.
The problem is that the commands were deprecated for some time which results in the error.
We fix the error by upgrading the JDBC MySQL driver to the latest version.
3. Mistyping and Missing of Data
The 1064 error occurs when the data is not found in the database or mistyping the commands.
In case, if the data is missing from the database, we manually add the data to the database. Also, we make sure that all the commands are spelled correctly.
[Need any assistance with SQL State 42000 Error codes? – We’ll help you]
Conclusion
In short, today we discussed in detail on MySQL Error code 1064 and saw how our Support Engineers find the fix for this error.
PREVENT YOUR SERVER FROM CRASHING!
Never again lose customers to poor server speed! Let us help you.
Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.
GET STARTED
var google_conversion_label = «owonCMyG5nEQ0aD71QM»;
I’m using latest version of MySQL ==> mysql-5.6.10-winx64.zip
Created the database and every thing is ok ‘I think’
when I try to execute this simple command;
"select * from family"
I got this error :
Error code 1064, SQL state 42000: 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 SQL_SELECT_LIMIT=DEFAULT’ at
line 1
I’ve spent much time searching for a solution but no solution was found
asked Feb 27, 2013 at 13:48
7
That looks like an error coming from a JDBC driver. When the JDBC driver initializes the connection, it sends several commands to the MySQL server, one of which is:
SET OPTION SQL_SELECT_LIMIT=DEFAULT
The problem is that the SET OPTION
syntax has been deprecated for some time and is now no longer valid in MySQL 5.6. Here’s a relevant bug conversation from MySQL’s bug database:
Bug #66659: mysql 5.6.6m9 fails on OPTION SQL_SELECT_LIMIT=DEFAULT
Try upgrading your JDBC MySQL driver. The bug conversation lists some other options in case upgrading the driver is not an option.
answered Feb 28, 2013 at 4:45
hruntinghrunting
3,84725 silver badges23 bronze badges
I followed the instructions above and this worked for me!
-
Download latest jar file from here:
http://dev.mysql.com/downloads/mirror.php?id=412737
Unzip it
Copy jar file «mysql-connector-java-5.1.25-bin.jar»
to this folder: C:Program FilesNetBeans 7.3idemodulesext -
In Netbeans IDE: Disconnect from database.
Click Services. Expand Drivers. Right-click on MySQL and press Customize.
Add latest driver
Remove previous driver. -
Re-connect to dabatase within IDE.
answered May 8, 2013 at 13:43
FreshFresh
1111 silver badge2 bronze badges
1
I had the same problem few weeks back. Followed the following steps and it very much resolved the issue.
-
Copied the latest version (mysql-connector-java-5.1.23-bin) of the jar file to ..NetBeans 7.3idemodulesext. My earlier version of the driver was mysql-connector-java-5.1.18-bin.
-
Change the driver version within NetBeans IDE. In the IDE’s Services window, expand Drivers -> right-click on MySQL (Connector/J driver) and select Customize. Remove the earlier driver and point it to the latest one (C:Program Files (x86)NetBeans 7.3idemodulesextmysql-connector-java-5.1.23-bin.jar). Click ok and restart IDE.
This should resolve the problem.
answered Apr 4, 2013 at 16:36
1
If the driver suggestion does not work, check your sql for unprintable characters. I just spent an hour troubleshooting this issue only to discover a hidden u+200b character at the end of my sql statement.
answered Oct 29, 2014 at 23:24
BriceBrice
3463 silver badges14 bronze badges
0
I got the same Error when i was dumping mysql table structure file in to DB. Mistake was putting the Syntax for dropping all table before but not checking their existence in the Database.
Issue was at » DROP TABLE usermgmt«.
I removed that code of lines of dropping tables and it proceeded with out any error this time.
answered Jan 7, 2016 at 6:42
1
I prefixed the name of the table with the database name such as
select * from database_name.table_name;
and it worked perfectly, so most likely there’s a name conflict.
answered May 2, 2017 at 11:31
Sqlstate 42000 Is a general code that come together with other number. Most often comes with the code 1064 and is related with SQL syntax error. This kind of error has been seen reported mostly on MySQL but also on other type of databases. This happen because your command is not a valid one within the “Structured Query Language” or SQL. Syntax errors are just like grammar errors in linguistics. In the following article we will try to explain the MySQL error 1064 but not only. Also we will show other error codes that comes together with Sqlstate[42000].
Full view of my sql error code 1064:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL (or any other like MariaDb) server version for the right syntax to use near (And here is the part of the code where the error comes)
sqlstate 42000 – mysql error 1064 – you have an error in your sql syntax
Other error codes related with Sqlstate 42000:
- 1 – syntax error or access violation 1055
- 2 – syntax error or access violation 1071 specified key was too long
- 3 – syntax error or access violation 1066 not unique table/alias
- 4 – syntax error or access violation 1068 multiple primary key defined
Understand and FIX MySQL error 1064 – sqlstate 42000
SQL 1064 means that MySQL can’t understand your command!
This type of error first need to be understood and after that you can fix it. The common causes of this error are:
- Upgrading MySQL or any other database to another version
- Using Wrong syntax that is not supported on your current version
- Error in applying the back tick symbol or while creating a database without them can also create an error
- Due to using reserved words
- Particular data missing while executing a query
- Mistyped/obsolete commands
If you see words like “near” or “at line”, you need to check for problems in those lines of the code before the command ends.
How do I Fix SQL Error Code 1064?
- Read the message on the error:
So in general the error tells you where the parser encountered the syntax error. MySQL also suggest how to fix it. Check the example below …..
- Check the text of your command!
In some cases the PHP commands has wrong lines. Create SQL commands using programing language can be the good example of this. So you will need to check and fix those commands. Use echo, console.log(), or its equivalent to show the entire command so you can see it.
- Mistyping of commands
The error can occur also when you misspell a command (e.g. instead of UPDATE you write UDPATE). This can occur often since are so easy to miss. To prevent this, make sure that you review your command for any typing error before running it. There are a lot of online syntax checkers that can help to debug your queries.
- Check for reserved words
Reserved words are words that vary from one MySQL version to another. Every version has its list of keywords that are reserved. They are used to perform specific functions in the MySQL engine. If you read the error and identified that occurred on an object identifier, check that it isn’t a reserved word (and, if it is, be sure that it’s properly quoted). “If an identifier contains special characters or is a reserved word, you must quote it whenever you refer to it.” You can find a full list of the reserved words specific for each MySQL version and their usage requirements at MySQL.com.
- Obsolete commands – another reason
Another possible reason for the sqlstate 42000 MySQL error 1064 is when you use outdated commands. As Platforms grow and change, some commands that were useful in the past are replaced by more efficient ones. A number of commands and keywords have been deprecated. This mean that they are due for removal, but still allowed for a short period of time before they turn obsolete. On cases that you have an older backup of a MySQL database that you want to import, a quick solution is to just search and replace “TYPE=InnoDB” with “ENGINE=InnoDB”.
- Particular data is missing while executing a query
If the relevant data missing from the database which is required for the query, you’re obviously going to run into problems. Using phpMyAdmin or MySQL Workbench you can enter the missing data. Interface of the application allow you to add the missing data manually to an appropriate row of the table.
You have an error in your sql syntax
“You have an error in your sql syntax” – Example 1
The error code generated jointly with the statement “syntax error or access violation”, “You have an error in your SQL syntax; check the manual that corresponds to your MySQL (or any other like MariaDB) server version for the right syntax to use near” and after that the part of SQL code where the issue is. So in simple way, the error view is showing you also where is the error. For example we have the error:
“Check the manual that corresponds to your MySQL server version for the right syntax to use near 'from, to, name, subject, message) VALUES ('[email protected]', '[email protected],com' at line 1”
So how to understand this?
from is a keyword in SQL. You may not use it as a column name without quoting it. In MySQL, things like column names are quoted using back ticks, i.e. `from`. Or you can just rename the column.
Another example of “You have an error in your sql syntax” sqlstate 42000 – Example 2
Error:
check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 [ SELECT COUNT(*) as count,region, MONTHNAME(date) asmonth FROM tempur_stores.stats WHERE date > DATE_ADD(DATE(NOW()), INTERVAL -1 WEEK) AND date < DATE(NOW()) GROUP BY region, MONTH(date ]
On the query:
$stmt = DB::query(Database::SELECT, 'SELECT COUNT(*) as `count`,`region`, MONTHNAME(`date`) as`month` FROM tempur_stores.stats WHERE `date` > DATE_ADD(DATE(NOW()), INTERVAL -1 WEEK) AND `date` < DATE(NOW()) GROUP BY `region`, MONTH(`date`');
The above query is missing a closing parenthesis in the query:
$stmt = DB::query(Database::SELECT, 'SELECT COUNT(*) as `count`,`region`, MONTHNAME(`date`) as`month`
FROM tempur_stores.stats
WHERE `date` > DATE_ADD(DATE(NOW()), INTERVAL -1 WEEK)
AND `date` < DATE(NOW())
GROUP BY `region`, MONTH(`date`');
---------- ^ right there
Just put a parenthesis ) before that apostrophe and it should work.
MariaDB error 1064 – Example 3
An example with MariaDB version issue. Trying to do example of tagging and when:
$id = Questions::create([ 'body' => request('title'), 'skillset_id' => request('skillsetId'), 'tags' => ['red', 'blue'] ])->id;
Getting error:
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 ‘>’$.”en”‘ = ? and `type` is null limit 1’ at line 1 (SQL: select * from `tags` where `name`->’$.”en”‘ = red and `type` is null limit 1)
Reason is that is using MariaDB and JSON columns are only supported by MySQL. Convert to MySQL to resolve the issue.
MariaDB error 1064
Fix error 1064 mysql 42000 while creating a database – Example 4
MySQL error 1064 can be appearing also while you are creating database using hyphen in the name like Test-Db. This can be solved by using back tick around the database name properly or remove the hyphen in the database name.
Example:
mysql> create database Test-DB;
You will get error:
ERROR 1064 (42000): 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 '-DB' at line 1
Solution:
mysql> create database ` Test-DB `;
So adding back tick around the database name will solve the issue.
Transfer WordPress MySQL database to another server
Exporting WordPress database to another server can also be cause the 1064 error. Can be resolved by choosing the compatibility mode and changing the database version to the current version you’re using. Please select the compatibility mode under the advanced tab when performing a backup and after that click the auto-detect file character set when restoring the MySQL database.
Read Also –
- Location of SQL Server Error Log File
- How to fix SQL Server Error 18456
- How to Restore Master Database
Conclusions:
The reason behind the error it’s related closely to the end of error message. We would need to see the SQL query to understand completely the issue you’re facing. So this is the reason that we can’t completely fix the MySQL error 1064 but we exposed some examples for you. You will need to review the documentation for the version of MySQL that you are having this error appear with and your syntax to fix the problem. There are multiple reasons for its cause. We suggest you perform the sqlstate 42000 error fixes if only has experience on MySQL database.
If you are working with MySQL 5.7 and you find an exception when storing a lot of fields with text format:
SQLSTATE[42000]: Syntax error or access violation: 1118 Row size too large (> 8126). Changing some columns to TEXT or BLOB or using ROW_FORMAT=DYNAMIC or ROW_FORMAT=COMPRESSED may help. In current row format, BLOB prefix of 768 bytes is stored inline.
And you are sure that the row format of the table is set to Dynamic, you are probably facing an issue with the innodb log file size and the strict mode. In this article, we’ll show you how to prevent this exception from appearing in MySQL 5.7.
1. Find my.cnf file
As first step, you will need to search for the configuration file of MySQL. There is no internal MySQL command to trace the location of this file, so the file might be in 5 (or more) locations, and they would all be valid because they load cascading:
/etc/my.cnf
/etc/mysql/my.cnf
$MYSQL_HOME/my.cnf
[datadir]/my.cnf
~/.my.cnf
Those are the default locations MySQL looks at, however if you still don’t find the correct file, you may run the following command on your terminal:
find / -name my.cnf
Once you find the file, open it with a CLI editor like nano and follow the next step.
2. Increase innodb_log_file_size value
In our case, the file is located at /etc/mysql/my.cnf
, so we could edit the file with nano using the file with the following command:
nano /etc/mysql/my.cnf
You will need to disable the strict mode of MySQL and increase the size of innodb log file. When innodb_strict_mode
is enabled, InnoDB returns errors rather than warnings for certain conditions. Like many database management systems, MySQL uses logs to achieve data durability (when using the default InnoDB storage engine). This ensures that when a transaction is committed, data is not lost in the event of crash or power loss. MySQL’s InnoDB storage engine uses a fixed size (circular) Redo log space. The size is controlled by innodb_log_file_size
. If you increase the value of this property, you will get rid off this exception when storing multiple columns of text in MySQL 5.7.
The theme about which size is right for the innodb log file won’t be covered in this article, instead we recommend you to read this article that contains a detailed explanation and facts about how to choose this value. As we are just sharing with you the solution to this problem, we’ll use the value of 512M
, so the parameters to add to the mysqld block of the my.cfn
file will be:
# Important: inside the mysqld block
[mysqld]
# Add new log file size
innodb_log_file_size=512M
# Disable strict mode
innodb_strict_mode=0
An example of how the file should look like:
#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
#
# * IMPORTANT: Additional settings that can override those from this file!
# The files must end with '.cnf', otherwise they'll be ignored.
#
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
[mysqld]
# Add new log file size
innodb_log_file_size=512M
# Disable strict mode
innodb_strict_mode=0
Although we didn’t cover a detailed explanation of the problem caused by the row size limitation that is removed by the dynamic row format, you may want to inform yourself about this problem visiting this article. After saving changes in the file , restart mysql with the cli depending of your os and installation process e.g:
# Ubuntu
sudo service mysql restart
# CentOS
/etc/init.d/mysqld start
Happy coding !