如何通过PHP检查MySQL中没有空格的字符串

时间:2015-10-21 09:47:02

标签: php mysql

我在PHP中有这个查询:

DELETE FROM table1 
where filename="'.$name.'" 
and id=(select id from table2 where cat="'.$category.'")

问题是字符串$ name没有空格,table1中的相对字段有它们。我该怎么做这个控制?

解决

我用MySQL replace()函数解决了它。 Trim()函数不起作用。

2 个答案:

答案 0 :(得分:2)

使用TRIM():

DELETE FROM table1 where TRIM(filename) = "'.$name.'" and id = (select id from table2 where cat="'.$category.'")'

MySQL TRIM: http://www.w3resource.com/mysql/string-functions/mysql-trim-function.php

答案 1 :(得分:2)

您可以使用修剪功能修剪列值。

DELETE FROM table1 where TRIM(filename) ="'.$name.'" and id=(select id from table2 where cat="'.$category.'")

http://www.w3resource.com/mysql/string-functions/mysql-trim-function.php