To save tearing any more of my hair out I thought I'd just pose the question here, as I'm having an infuriating time with PHP Mysql UPDATE: something I use quite a lot and thought I understood!
Basically, are there any table header names that are known to break MySQL update functions? I have an Update mysql_query function that works perfectly, for example:
UPDATE table_name SET
part_number='000 - New Product',
product_code='1',
barcode_ref='1',
type='new type'
WHERE id='999'
However, if I include the table header called 'trigger' in the code it breaks it!
UPDATE table_name SET
part_number='000 - New Product',
trigger='YES',
product_code='1',
barcode_ref='1',
type='new type'
WHERE id='999'
The above sql returns an error of: **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
'trigger='YES', part_number='000 - New Product', product_code='1', barcod' at line 1**
I have made an identical duplicate of the column called 'trigger' and re-named it 'testing_t' and immediately everything works perfectly as before. I've tried both dumping my mysql_real_escape_string to variables for use in the UPDATE command and doing them inline, and even tried hard-coding the string and it still breaks.
Can anyone shed any light on this? Ideally I'd really like to not have to change my table header name, as I don't want to modify the references to it across the site. Obviously if there is no other option then I will, but I'm hoping I'm just being stupid and that someone can explain why it's happening/how to stop it happening!
Thanks in advance,
Joe
use this query
UPDATE table_name SET
`part_number`='000 - New Product',
`trigger`='YES',
`product_code`='1',
`barcode_ref`='1',
`type`='new type'
WHERE id='999'
your query will fail because you have written trigger without ``
trigger is reserved word in mysql for creating triggers.
To use reserved word as column name you have to write that word inside ``
trigger is a mySQL keyword. If you enclose all column names in `` you will be safe. It's good practice to not use these keywords as column names.
Other keywords are here http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html
mySQL has a series of reserved words, of which TRIGGER is one. Consult this list of words as a guide for what NOT to call your columns/tables:
http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html
Related
The following query:
select replace(`Abilities`, 'export_import', 'auto') from fl_account_types;
gives me 'auto,listings' correct replacement from Abilities column. However, when I execute:
update fl_account_types set `Abilities` = replace(`Abilities`, 'export_import', 'autos');
MySQL just omits 'export_import' string and replaces Abilities with 'listings' string.
What could be the reason?
The problem was that Abilities was of type SET and I was trying to replace with a value which was not listed in a definition of it. But I still do not understand why select replace works well and why MySQL do not throw an error.
I try to remove new line symbols from text type field, statement I use - below, tried all statements I found here with no luck:
UPDATE `ae1_jshopping_products`
SET `short_description_lt-LT` = REPLACE(`short_description_lt-LT`, '\r\n', ' ');
I also attach print with error messages.
Thanks in advance!
put your fieldname short_description_lt-LT into backticks:
UPDATE `ae1_jshopping_products`
SET `short_description_lt-LT` = REPLACE(`short_description_lt-LT`, '\r\n',' ');
explanation: MySQL can't know if you want to subtract LT FROM short_description_lt or if - belongs to your fieldname.
I can't reproduce the problem. Perhaps there is a hidden character that may be causing the problem.
Using:
MariaDB: 10.0.22
phpMyAdmin: 4.6.0
Your outdated phpMyAdmin version is affected by a bug with the SQL parsing library where it thinks your statement is incorrect. You should be able to solve it by upgrading to a more recent version (4.6.1 is expected to be released within a week).
mysql php admin table query enter image description here
I don't see why it says partID is giving me a problem? it is in the table and i think i have them linked correctly. I Have changed a few things i replaced comma with the and statement which cleared up alot of my errors. But since I've done that it continues to give me #1305 - FUNCTION homeshopping.partID does not exist. I have even looked at the structures an designer to make sure column names an tables are correct.
Don't forget to post your query as text also.
Because it's missing the keywork IN to have a query like this:
SELECT CONCAT(hscust.first,' ', hscust.last AS Customer,hsitems.description,hsitems.price,hsitems.itemCalss
FROM hscust,hsorders,hslineitem,hsitems
WHERE hslineitems.orderId = hsorders.orderId AND hsitems.partID = hslineitem.partNum AND hslineitem.price = hsitems.price AND partID IN ('CB03', 'CZ82');
I need to run a regex find-and-replace against a column named message in a MySQL table named post.
My database is running MariaDB 10.
According to the docs, MariaDB 10 has a new REGEXP_REPLACE function designed to do exactly this, but I can't seem to figure out the actual syntax.
It will affect 280,000 rows, so ideally there's also a way to limit it to only changing one specific row at a time while I'm testing it, or simply doing a SELECT rather than an UPDATE until I'm sure it does what I want.
The regex I want to run:
\[quote\sauthor=(.+)\slink=[^\]]+]
The replacement string:
[quote="$1"]
The following was what I tried, but it just throws a SQL error:
UPDATE post SET message = REGEXP_REPLACE(message, '\[quote\sauthor=(.+)\slink=[^\]]+]', '[quote="$1"]') WHERE post_id = 12
In this case, the original message was:
[quote author=Jon_doe link=board=2;threadid=125;start=40#msg1206 date=1065088] and the end result should be [quote="Jon_doe"]
What is the proper syntax to make this REGEXP_REPLACE work?
You have to do a lot of escaping here:
REGEXP_REPLACE(message, "\\[quote\\sauthor=(.+)\\slink=[^\\]]+]", "\\[quote=\"\\1\"\\]")
Please note that you have to reference the Group by \\1
Some days ago I asked a question about my problem and I was advised to use CONCAT_WS function. I am using CONCAT_WS on my local mysql database and it is working perfectly. But it is not working on server(application hosted) and generate the following error.
FUNCTION test.CONCAT_WS does not exist
Here test in error string is my database name on server.
My query is like this:
SELECT * FROM patient WHERE CONCAT_WS (',', LastName,FirstName,BirthDate ) NOT IN ('Abdul,Quddus,2000-09-30','Wasim,Akram,1993-09-12');
Can someone tell me the problem or suggest me another solution asked in linked question above ?
Thanks
The easiest way to fix it is by removing the whitespace between the function name and the parenthesis, i.e. CONCAT_WS(...) instead of CONCAT_WS (...).
From the MySQL Manual:
By default, there must be no
whitespace between a function name and
the parenthesis following it. This
helps the MySQL parser distinguish
between function calls and references
to tables or columns that happen to
have the same name as a function.
...
You can tell the MySQL server to
accept spaces after function names by
starting it with the
--sql-mode=IGNORE_SPACE option.
Also, this behavior depends on the MySQL version, this is why it works on one server and doesn't work on another, quote from the "Function Name Parsing and Resolution" manual page:
The number of function names affected
by IGNORE_SPACE was reduced
significantly in MySQL 5.1.13, from
about 200 to about 30.