在SELECT语句中使用replace

时间:2011-11-23 11:26:14

标签: php mysql replace

我想从我的一个数据库字段中删除任何中断标记,并用空格替换它。 所以我尝试了以下内容:

$select = SELECT ID, REPLACE(fieldname, "<BR>", " ") FROM mytable

但这会导致意外&gt;错误。我也试过'<BR>',但这只是让csv中的字段名称更改为REPLACE(fieldname, '<BR>', ' ')

任何可以指引我朝正确方向前进的人?

2 个答案:

答案 0 :(得分:2)

mysql_query()怎么样?

$select = mysql_query('SELECT ID, REPLACE(fieldname, "<BR>", " ") FROM mytable');

<强>更新

我是从phpMyAdmin

尝试过的
CREATE TABLE `mytable` (
`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`fieldname` TEXT NOT NULL
) ENGINE = MYISAM ;

INSERT INTO `usr_web106_38`.`mytable` (`fieldname`)VALUES ('test <BR> test <BR>');

查询有效:

SELECT ID, REPLACE( fieldname, "<BR>", " " ) FROM mytable;

ID  REPLACE(fieldname, "<BR>", " ")
1   test   test  

PHP也是如此

$select = mysql_query('SELECT ID, REPLACE(fieldname, "<BR>", " ") FROM mytable');
echo "<pre>"; var_dump(mysql_fetch_array($select) ); echo "</pre>";

结果

array(4) {
  [0]=>
  string(1) "1"
  ["ID"]=>
  string(1) "1"
  [1]=>
  string(13) "test   test  "
  ["REPLACE(fieldname, "
", " ")"]=>
  string(13) "test   test  "
}

你的问题可能是什么。它对我们来说是不可见的。

答案 1 :(得分:0)

<?php 
$select = SELECT ID, REPLACE(fieldname, "<BR>", " ") FROM mytable

//1 : Syntax Error no quotes & semicolon

$select =  'SELECT ID, REPLACE(fieldname, "<BR>", " ") FROM mytable';

//2 : you've to run query 

mysql_query($select);
?>