长话短说,我正在尝试编写一个将文本文件解析为 MySQL 查询的 PHP 代码。除了由 UPDATE 语句组成的查询外,一切正常。
整个代码有点长,但如果你想看看 - http://pastebin.com/xVR6ArD0
这只是有问题的部分:
while ($i<=$no_collumns)
{
$j = $i-1;
if (!
mysql_query
("UPDATE ResultsPredmet
SET ${parsed_collumns[$i]} = '${parsed_words[$j]}'
WHERE ${parsed_first_collumn} LIKE '${parsed_first_word}'")
)
{echo mysql_error()."\n"; break;}
// echo "\nUPDATE ResultsPredmet SET ${parsed_collumns[$i]} = '${parsed_words[$j]}' WHERE ${parsed_first_collumn} LIKE \"${parsed_first_word}\"";
$i++;
}
... 其中 $parsed_collumns 和 $parsed_words 是字符串数组, $parsed_first_collumn 和 $parsed_first_word 是字符串。
我尝试了变量的所有引号和转义组合。我尝试将它们放在双引号中并转义它们,或者双引号并将它们连接起来,然后也许我认为这是我通过'='运算符比较字符串的事实,所以我尝试使用'LIKE'。我用谷歌搜索了几个小时,到处有人说要对变量使用单引号,所以我也尝试过,但没有成功。
最后,我回应了这些查询,我得到:
UPDATE ResultsPredmet SET grade = '10' WHERE name LIKE "Vildur"
UPDATE ResultsPredmet SET index = '117/2010' WHERE name LIKE "Vildur"
Updating table.
UPDATE ResultsPredmet SET grade = '6' WHERE name LIKE "NinoDoko"
UPDATE ResultsPredmet SET index = '132/2011' WHERE name LIKE "NinoDoko"
Updating table.
UPDATE ResultsPredmet SET grade = '10' WHERE name LIKE "Koco"
UPDATE ResultsPredmet SET index = '130/2011' WHERE name LIKE "Koco"
Done.
这对我来说似乎相当不错。我得到的其他查询仅与带有单引号的名称或没有引号或任何其他组合的名称相同。
我得到的错误是:
Updating table.
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'index = '117/2010' WHERE name LIKE 'Vildur'' at line 1
Updating table.
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'index = '132/2011' WHERE name LIKE 'NinoDoko'' at line 1
Updating table.
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'index = '130/2011' WHERE name LIKE 'Koco'' at line 1
显然,我使用的服务器是 MariaDB 5.5,但经过一些研究后,我认为它与通用 MySQL 相似,尽管我可能完全不使用。“更新表”。只是我代码中的随机回声。我也尝试过没有缩进的查询,仍然有同样的错误。grade
我得到的值index
是字符串——或者至少我希望如此,因为我用explode()
.