SQLSTATE [42000] when adding data - php

When parsing an xml file and adding it to the database, it displays an error during the script operation:
$sql->exec("INSERT INTO 'example' VALUES('$id', '$title', '$link')");
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 ''example'
VALUES('phpmysqljquery-przeciagnij-z-wy'
at line 1
Can anyone say what is wrong?

You're using the wrong quoting style. Database, table and column identifiers use a different approach:
INSERT INTO `example` VALUES (?,?,?)
Double and single quotes are only for strings. You can't insert stuff into a string.

Related

Why $wpdb->insert() makes sql syntax error?

I am using wpdb->insert to insert a row to the table,
like this
$wpdb->insert($table_name, array("title"=>"foo"));
$wpdb->last_query returns the below query
INSERT INTO `some_table` (`title`) VALUES ('foo')
But i get a error in sql syntax
WordPress database 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 '1' at line 1
The error is little confusing, i passed a string 'foo' in to insert statement, but somehow it is showing '1' in the error.
This error occurs when i am running the $wpdb->insert inside phpunit, i copied the query and ran it on my test wordpress database and this query works correctly in the different method in the same test suite, it works fine.Any idea how to fix this error?
Please try this code. The recommended way (as noted in codex):
$wpdb->insert($table_name,array('title'=>'foo'),array('%s'));

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

I have renamed my database in phpMyAdmin but it makes some problems. When I tried to rename it to its first name, it asked first if I want to rename it and drop my database.
When I press OK, it give me 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 qetary2015-02-21.gps_poss
The SQL query:
ALTER TABLE qetary2015-02-21.gps_poss
How can I fix it please?
At a guess:
ALTER TABLE `qetary2015-02-21`.gps_poss
Wrap the database name in backticks, else the - character might be misinterpreted as a minus sign.... though I hope there's slightly more to your SQL query than that snippet

Syntax error or access violation using OUTPUT keyword

I'm attempting to make use of the OUTPUT line so I don't have to use multiple queries to get information that I've inserted into a table. This is for a basic image uploader project that will rename the images based on their id in the database.
Here's the query I'm using (Using it with PDO)
INSERT INTO `images` (`id`, `type`, `category`, `title`) VALUES (null, :type, :cat, 'Newly uploaded image') OUTPUT INSERTED.id;
-
Processing upload
Error uploading file: 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 'OUTPUT INSERTED.id' at line 1
I've read over a few different tutorials now and I'm really confused as to what the problem is.
OUTPUT is not working in MYSQL.
Use
SELECT LAST_INSERT_ID();
after your insert query

MySql query - syntax error

The following code is an SQL query that I am using in PHP.
$sql = "INSERT INTO updates (update,user_id_fk)
VALUES ('$post_update','$username')";
It does not work - I get the following error message when it is run.
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 'update,user_id_fk)
This syntax loks fine to me - it is the same as I found on W3Schools.
The mysql verion I am using is 5.5.24
update is a reserved keyword backtick it
INSERT INTO updates (`update`,user_id_fk)
check the list below and in future do not use these words for your table or column names
http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

PHP syntax error adding data to MySQL db

trying to add data to mySQL db.
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 'MATCH(time, date, location, weather, team_id) VALUES('t', 't', 't','t','2')'
this is the PHP code snippet:
$sql = "insert into MATCH(time, date, location, weather, team_id) VALUES('$time', '$date', '$location','$weather','$team_id')";
I cant see any syntax errors
MATCH is a reserved for a function used in fulltext search:
http://dev.mysql.com/doc/refman/5.5/en/fulltext-search.html
That's not a php syntax error. It's a Mysql syntax error. I suggest changing the table's name.
Try
$sql = "INSERT INTO `MATCH` (`time`, `date`, `location`, `weather`, `team_id`) VALUES ('".$time."', '".$date."', '".$location."','".$weather."','".$team_id."')";
Using the backtick character ` you can distinguish names you gave to your table or columns from reserved words of the MySQL language. Leaving them out might seem more compfortable at first, but can be a pain later.
E.g. one should know that mysql syntax is not case sensitive. So even if you write match you will get this problem. A list of the reserved words can be found at the link Mark gave you in his comment.
You might also want to read up on MySQL Syntax in general:
http://dev.mysql.com/doc/refman/5.1/en/sql-syntax.html

Categories