I am trying to use UPDATE with my MySQL-Database. I use the following SQL code:
$sql = "UPDATE ToDo
SET Checked = -1
WHERE Index = 1";
When I use this code i get the following error message: "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 'Index = 1' at line 3"
But when I use
$sql = "UPDATE ToDo
SET Checked = -1
WHERE Text = 'asdf'";
Everything works.
My database has one table named "ToDo" with 3 collumns: Index(int, primary key, auto_increment), Checked(bool) and Text(text).
Can't you "WHERE" a primary key or did i forget something else?
Hope you can help me.
Try adding the backticks:
UPDATE ToDo
SET `Checked` = -1
WHERE `Index` = 1";
Index is a reserved word :
http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html
index is a reserved word for MySQL. You need to esacpe the name by adding backticks like this:
$sql = "UPDATE ToDo
SET Checked = -1
WHERE `Index` = 'asdf'";
In order to make sure MySQL understands that you are talking about a column name and not the reserved word, you can always address the column name tablename.columnname.
In SELECT queries also using shortcuts is possible:
UPDATE ToDo SET ToDo.Checked = -1 WHERE ToDo.Index = 1
SELECT u.Index FROM users u
Also I would recommend not to use camel cases in tables and columns. This proved to be a source for errors and has no real benefit most of the time.
Boolean is 0 or 1, not -1. Try using 0 or 1 for Checked and let us know what happens.
Related
Man, I don’t know why this isn’t working, i’m following the Manuel verbatim.
I have a ProductStyles Table that has an order column. Suppose i have 5 rows. And I delete number 3. I want to find all rows matching the product id and iterate through each one of them replacing the order # incrementially.
Productstyle Table
id - product_id - order
My first step is to just fetch all the rows matching product ID in order so i can do a foreach but I keep get SQL errors. Honestly I don’t know SQL that well other than the basic select.
$query = $this->ProductStyles->find()
->where(['product_id'=> 1 ])
->order(['order' => 'ASC'])
;
//debug($query);
//debug( $query->all() ); //this throws an error
Error Message:
Error: 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 'order ASC' at line 1
SQL:
//SELECT ProductStyles.id AS `ProductStyles__id`, //ProductStyles.product_id AS
//`ProductStyles__product_id`, ProductStyles.style_id AS //`ProductStyles__style_id`,
//ProductStyles.order AS `ProductStyles__order` FROM product_styles ProductStyles
//WHERE product_id = :c0 ORDER BY order ASC
//die;
$i = 1;
foreach ($query as $row) {
//debug($row); die;
$ps= $this->ProductStyles->get($row->id);
//debug($ps);die;
$ps->order = $i;
$this->ProductStyles->patchEntity($ps,['order'=> $i]);
$this->ProductStyles->save($ps);
$i++;
}
order is a reservered word, and is not allowed to be used in an unquoted (in this case non-backticked) fashion, it would need to be used in the form of `order`
See
https://dev.mysql.com/doc/refman/5.7/en/keywords.html
https://dev.mysql.com/doc/refman/5.7/en/identifiers.html
I would suggest to change the column name, that's the easiest, and non-performance affecting fix. Another option would be to enable automatic identifier quoting, this will however affect the performance, and cannot be applied everywhere, see
Cookbook > Database Access & ORM > Database Basics > Identifier Quoting
order is a reserved keyword in SQL, so, if you want to use as a column name, you need to use quotes, like this:
SELECT * FROM table ORDER BY `order` ASC
As you can see in the error, is not being quoted.
Maybe you have to use $this->Model->query() and write the query manually.
Another solution is change the "order" column to another name.
Hope this helps.
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 'WHERE `id_product` = 9' at line 1
UPDATE `ps_product` SET `price` = WHERE `id_product` = 9
The value for price is missing in your sql:
UPDATE `ps_product` SET `price` = WHERE `id_product` = 9
^^^^
You need to pass empty value with in single or double quotes or any values
UPDATE `ps_product` SET `price` = '' WHERE `id_product` = 9
The Problem in your query is that you have "price = WHERE ". As you didnt paste your whole code there I guess that the variable which holds the value with to which Price shall be set is empty.
Thus you should control that variable and see why it is empty (in case the whole query is a string with no variables involved then you forgot the value to which Price shall be set).
If Price is meant to be empty you will have to set it to an empty value via either using ='', =null or =0 depending on what empty is represented by the field (and its type).
I am trying to insert a column with column name 00:18:e7:f9:65:a6 with the statement
ALTER IGNORE TABLE tblwifi_information ADD "00:18:e7:f9:65:a6" INT
...but it throws an error:
/* SQL 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 '"00:18:e7:f9:65:a6" INT' at line 1 */
in Heidi SQL. I am using MySQL database.
When I try to add the column manually b going to the structure of the table it works and does not give any error but when I run this statement it does not allow me. How can I solve this?
I just ran your query:
ALTER IGNORE TABLE tblwifi_information ADD "00:18:e7:f9:65:a6" INT
and got the same error. I then ran:
ALTER IGNORE TABLE tblwifi_information ADD `00:18:e7:f9:65:a6` INT
and it worked. Pretty sure you need to change " to `
Fail with double qoutes:
Success with backticks:
use backticks for columnname
change
ALTER IGNORE TABLE tblwifi_information ADD "00:18:e7:f9:65:a6" INT
TO
ALTER IGNORE TABLE tblwifi_information ADD `00:18:e7:f9:65:a6` INT
SQL FIDDLE
To add something to the story, when querying multiple tables, you have to use following expression:
`tablename`.`columnname`
(NOT `tablename.columnname` )
I took me some time to figure it out, so I hope it helps someone :)
Example:
SELECT *
FROM tb_product p
JOIN tb_brand b ON ( `p`.`tb_brand:IDbrand` = `b`.`IDbrand` )
WHERE IDfamily = 2
i dont really know what you are trying to achive here pal.
instead of:
00:18:e7:f9:65:a6
to
00_18_e7_f9_65_a6
then adjust your code to replace ":" to "_" in php or mysql.
in sql:
Replace(table_name, ':', '_');
in PHP:
srt_replace(':','_',$query);
I would like to combine two columns in one column as Fullname, and for that I have written the following code:
$this->db->select('CONCAT(first_name," ",last_name) AS FullName');
$this->db->from('customer');
$this->db->where('user_id',1);
$query = $this->db->get();
return $query->result_array();
The resulting query would be:
SELECT CONCAT(first_name," ",last_name) AS FullName
FROM customer
WHERE user_id = 1
but when i execute the above code it gives me:
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 'FROM (customer) WHERE user_id = '1' at line 2
I have also tried with concat_ws group_concat but not able to get it work. Can anyone see what I'm doing wrong?
By default, CI tries to escape what you pass to db->select() (in case you were passing in user-generated values). You can disable this feature by passing false as a second argument.
$this->db->select('CONCAT(first_name," ",last_name) AS FullName', false);
I have been through this before with CI, in my case CI was wrongly creating the query, for example putting simgle quotes where they shouldn't be.
My advice create the query yourself and run it, you could be surprise :P
This seems to be a really simple query, but somehow I keep getting errors...
Basically, I just got a bunch of information from a user, and now I'm going to update their record in the users table in one query:
UPDATE users SET timezone = 'America/New_York', SET updates = 'NO', SET verified = 'YES' WHERE id = '1'
However, after running that, I get 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 updates = 'NO', SET verified = 'YES' WHERE id = '1'' at line 1".
Any help is much appreciated.
UPDATE users SET timezone = 'America/New_York', updates = 'NO', verified = 'YES' WHERE id = '1'
Your update syntax is wrong, you have to write syntax SET just once.
UPDATE users SET col1= value1, col2= value2, col3= value3 WHERE condition;
More information about update
UPDATE MANUAL
Set have to be used once no matter how many columns you are updating .your query will be :-
UPDATE users SET timezone = 'America/New_York', updates = 'NO',verified = 'YES' WHERE id = '1'