How to properly save an image URL in Mysql database?

Asked

Viewed 2,437 times

6

I am testing the targeting of images with the URL. However, during the tests, the URL was returned with changes as can be seen below:

URL inserted in the database:

http://www.meusite.com.br/pastaimagem/logos/imageLogo.jpg

URL returned by database:

http:\/\/www.meusite.com.br\/pastaimagem\/logos\/imageLogo.jpg

I noticed this problem while testing the query by browser.

Version of PHP:

PHP: 5.4.37

Query used to insert URL:

UPDATE  `meusite_sitebd`.`jump` SET  `urlimagem` =  'http://www.meusite.com.br/pastaimagem/logos/imageLogo.jpg' WHERE `register`.`idRegistro` =1;

PHP code used to perform the request:

<?php

include 'conexao.php';

//Converte para UTF8 os resultados da query
mysql_set_charset('UTF8');

// 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);


mysql_close(); 

?>

As you can see it’s a very simple code.

  • No. I’m saving only her URL. I’ve actually edited the question. It’s gotten a little confusing, but now it’s more readable. The image is in a directory on the server itself.

  • Okay, how are you saving the URL?

  • On the Phpmyadmin screen itself, I edit the record and copy and paste the URL. But on the PHP screen the URL is normal. Only appears with the counterbars when I receive them by request.

  • Is Magic Quotes enabled? What version of php? Could you put the code of Insert

  • I edited the question and added the code used to update the record and insert the PHP URL and version. I will check the Magic Quotes...

  • You don’t happen to have a code addslashes putting these bars?

  • No. This is the first time I’ve encountered this problem. :/

  • I’ll add the code I’m using to the query...

Show 3 more comments

2 answers

5


3

@Tiago Amaral saw that you set your database to "UTF8" charset in php but your database was created with this same charset? in php your charset tbm is "UTF8"? If that’s where the problem might be, check it out. put this in php to set the charset of the same.

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

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

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');
  • Yes the field in the database is as UT8. And in PHP tbm... In case it would be a certain redundancy. I will remove the conversation in php to test.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.