显然,当从PHP发送时,此查询中存在SQL语法错误,但它在PHPMyAdmin上运行正常

显然,当从PHP发送时,此查询中存在SQL语法错误,但它在PHPMyAdmin上运行正常

问题描述:

It's all in the title really.

I've got this SQL query, ugly I know, but it should at least work... It works when I use it in PHPMyAdmin but when sent from PHP I get a syntax error from MySQL. :(

INSERT INTO `shortname_revisions` (old_shortname , new_shortname) VALUES ('$old', '$new');

UPDATE `soft_data` SET shortname = REPLACE(shortname ,'$old', '$new');
UPDATE `shot_data` SET shortname = REPLACE(shortname ,'$old', '$new');
UPDATE `virus_scanning` SET shortname = REPLACE(shortname ,'$old', '$new');
UPDATE `lang_translations` SET shortname = REPLACE(shortname ,'$old', '$new');
UPDATE `ratings` SET shortname = REPLACE(shortname ,'$old', '$new');
UPDATE `file_data` SET shortname = REPLACE(shortname ,'$old', '$new');

The $new and $old variables when pasted into PHPMyAdmin to test, would contain any string. Use $old = "media-player"; $new = "video-player"; as examples.

Also, I'm not sure of the correct use of the back ticks and where they should be and when, maybe that's part the problem.

For everyone asking about the error, sorry, I didn't post it. And I sure ain't about to go and recreate the error to paste it here. It was a pretty generic "You have a syntax error in roughly this square quarter mile of code somewhere" error.

这一切都在标题中。 p>

我有这个SQL 查询,丑陋,我知道,但它至少应该工作...当我在PHPMyAdmin中使用它时它工作但是当从PHP发送时我从MySQL得到语法错误。 :( p>

  INSERT INTO`shortname_revisions`(old_shortname,new_shortname)VALUES('$ old','$ new'); 
 
UPDATE`soft_data` SET shortname = REPLACE  (短名称,'$ old','$ new'); 
UPDATE`shot_data` SET shortname = REPLACE(shortname,'$ old','$ new'); 
UPDATE`virus_scanning` SET shortname = REPLACE(shortname,'  $ old','$ new'); 
UPDATE`lang_translations` SET shortname = REPLACE(shortname,'$ old','$ new'); 
UPDATE` ratings` SET shortname = REPLACE(shortname,'$ old',  '$ new'); 
UPDATE`file_data` SET shortname = REPLACE(shortname,'$ old','$ new'); 
  code>  pre> 
 
 

$ new code>和 $ old code>变量粘贴到PHPMyAdmin进行测试时,将包含任何字符串。使用 $ old =“media-player”; $ new =“video-player” ; code>作为例子。 p>

另外,我不确定正确使用后面的刻度以及它们应该在何时以及何时,这可能是问题的一部分。 p>

对于每个询问错误的人,对不起,我没有发布。我肯定不会 去重新创建错误以将其粘贴到此处。 这是一个非常通用的“你在大约这个四分之一英里的代码中出现语法错误”错误。 p> div>

1) Each statement needs to be sent in a separate mysql_query() call.

2) The backticks are fine but unecessary as there are no embedded blanks in any of those table names

3) I don't know your data structure but UPDATE without a "where" clause could be considered to be slightly unusual (unless these are intentionally single-row tables)

I tend to always removed back ticks just to keep my code consistent, but this won't make a difference and will not be causing the error.

If the code is working in PHPmyadmin then it could to be a problem with $new and $old, try testing it change $new and $old to be set values, e.g. $new="newtest" and $old="oldtest", see if that works, if it does you know the error with your variables, post them if that is the case.

Also, you may want to just try one update, it may be that because you are trying to update all together in one query you are getting an error.