MySql query - syntax error - php

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

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'));

my sql select query with table name in french language

I have a table named aset_catégorie and when I try to run a select query I'm getting 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 '©gorie' at line 1
How do I solve this?
SELECT * FROM `aset_catégorie`
You might have missed
` (backtick)
sign...
Hope this helps

Insert single numerical value into MySQL column [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 7 years ago.
The query I'm trying to run is:
INSERT INTO albumtest (on) VALUES ('3')
Server type: MySQL
Server version: 5.6.21
Syntax 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 'on) VALUES ('3')' at line 1
I've been looking over this for almost a day now, and I can't seem to figure out why I'm getting this syntax error.
Full code is here: http://pastebin.com/6mMbZ1Y1
But I know the rest of it is fine, because it can run other queries fine (such as $sql = "INSERT INTO gallery (title) VALUES ('".$title."')";)
this is the correct syntax
INSERT INTO `albumtest` (`on`) VALUES ('3')
ON is reserved keyword of mysql it should be write inside backtics
INSERT INTO albumtest (`on`) VALUES ('3')
on is MySql reserved keyword. Dont use it as column name.

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

MySQL query working in phpmyadmin but not in PHP

i've created a mysql view which is expecting 2 parameters and i'm able to query it without problems in phpyadmin with the following sql-string:
SET #date1 = '2014-02-06';
SET #date2 = '2014-02-07';
SELECT * FROM _myquery
it will not work under php - i'm getting 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 'SET #date2 = '2014-02-07'; SELECT * FROM _myquery' at line 2
when it's working in phpmyadmin, shouldn't it also work under php?
any idea what's wrong?
Do you really need the MySQL variables? because your SELECT query is not using them.
You can try just with
SELECT * FROM _myquery
But if you really need to use MySQL variables, take a look at this:
Mysql Variables not working through php mysql query
MySql variables and php

Categories