How to correctly save an image URL in MySQL database?


I'm testing the targeting of images with the URL. But during the tests, the URL was returned with changes as seen below:

URL entered in the database:

URL returned by the bank:


I noticed this problem while testing the query by the browser.

PHP version:

PHP: 5.4.37

Query used to enter URL:

UPDATE  'meusite_sitebd'.'jump' SET  'urlimagem' =  '' WHERE 'register'.'idRegistro' =1;

PHP code used to perform the request:


include 'conexao.php';

//Converte para UTF8 os resultados da query

// Retorna registro por id

$id = $_GET['idRegistro'];

$sql = "SELECT * FROM registros WHERE idRegistro = '$id'";

$resultado = mysql_query($sql) or die ("Erro: ".mysql_error());

// Crinha variável linha do tipo array
$linha = array();

  while($r = mysql_fetch_assoc($resultado))
        $linha [] = $r;

 echo json_encode($linha);



As you can see, it's a very simple code.

asked by anonymous 23.02.2015 / 21:33

2 answers


By default json_encode places bars to escape some characters, to avoid this behavior, pass JSON_UNESCAPED_SLASHES in the second argument of the function so the escape bars will not be added.

echo json_encode($url,  JSON_UNESCAPED_SLASHES);

Example - ideone

Based on: How to remove backslash on json_encode () function?

23.02.2015 / 23:27

@Tiago Amaral I saw that you set your bank to charset "UTF8" in php plus your bank was created with this same charset? in php your charset tbm is "UTF8"? otherwise that's where the problem is, check it out. put this in php to set the charset of it.

header("Content-Type: text/html; charset=UTF-8", true);

in the connection file with the bank put this to see if it resolves.

mysql_query("SET NAMES 'utf8'");
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_results=utf8');
23.02.2015 / 22:53