Ebi Nadian
Ebi Nadian

Reputation: 53

Mysql Update and Replace Syntax in PHP

I need to eliminate the character (") from a column in my database. I can do in mysql command line by the following command:

mysql> UPDATE tName SET colName=REPLACE(colName, '"','');

and it works perfectly. Since i need run in php, i have used the following syntax in php but it dosent work :

$sql0 = "UPDATE tName SET colName=REPLACE(colName,'"','')";
if (mysqli_query($conn, $sql0)) {
$result0 = "Unwanted Character is removed ";
} else {
$result0 = "Error Filtering is Failed: " . $sql . "<br>" .       mysqli_error($conn);
}

any Idea??

Upvotes: 1

Views: 406

Answers (3)

devpro
devpro

Reputation: 16117

You can use quotes in REPLACE function as:

$one = '"';
$sql0 = "UPDATE tName SET colName = REPLACE(colName,'$one','')";

If you echo $sql0 result is:

UPDATE tName SET colName = REPLACE(colName,'"','')

Upvotes: 1

Budianto IP
Budianto IP

Reputation: 1297

Try this one instead :

$sql0 = "UPDATE tName SET colName=REPLACE(colName,'\"','')";

notice there is a back slash :)

Upvotes: 3

Franz Gleichmann
Franz Gleichmann

Reputation: 3568

you have to escape double quotes inside double-quoted strings.

$sql0 = "UPDATE tName SET colName=REPLACE(colName,'\"','')";
if (mysqli_query($conn, $sql0)) {
  $result0 = "Unwanted Character is removed ";
} else {
  $result0 = "Error Filtering is Failed: " . $sql . "<br>" . mysqli_error($conn);
}

Upvotes: 2

Related Questions