the result of a different mysql replace if I do it with php or with phpmyadmin

0

This is my code:

UPDATE basedatos.tabla 
             SET columna= REPLACE (columna,'\\SERVER','D:xampp\htdocs')
             WHERE id=1

The result in phpmyadmin is D:xampp\htdocs. It is correct

And in php write me \D:xampphtdocs. Does anyone know why?

Thanks

    
asked by JOSE MARIA 29.07.2018 в 19:08
source

1 answer

0

As they say in the comments, when writing that query (as a string) you must escape the% bar_counds of% again.

This is your query as a string in php:

$consulta = "... columna= REPLACE (columna,'\\SERVER','D:xampp\htdocs') ..."

As in the php strings, the backslash is used to escape, the real query that arrives in the database is:

\

But in addition, the REPLACE function can receive strings / literals as parameters, so it also uses the backslashes to escape quotes and other backstops.

So, this SQL:

... columna= REPLACE (columna,'\SERVER','D:xampp\htdocs') ...

It really means: Replace in "column", this: "\ SERVER" with "D: xampphtdocs"

(In the second parameter the backslash disappears because there is only one and within the string there should be 2 because the backstops must escape)

So if the column is worth ... columna= REPLACE (columna,'\SERVER','D:xampp\htdocs') ... and replace \SERVER with \SERVER , we have what you get: D:xampphtdocs

To solve this, try escaping the backslashes again:

$miconsulta = "
UPDATE basedatos.tabla 
  SET columna= REPLACE (columna,'\\\\SERVER','D:xampp\\htdocs')
  WHERE id=1
";
    
answered by 03.08.2018 в 13:33