0

Hello guys im with a problem handling PHP and JSON

This is my php code

<?php

    mysql_connect("127.0.0.1","root","");
    mysql_select_db("spadramatico_db");

    $query = mysql_query("SELECT * FROM feed_table ORDER BY id");
    $records = array();

    while($obj = mysql_fetch_object($query)) {
        $records [] = $obj;
    }
    print (json_encode($records));

?>

And this is the output result:

[{"id":"1","title":"Teste Title","image":"http:\/\/catalinaseaspa.com\/wp-content\/uploads\/2015\/03\/island-girl.jpg","desc":"Desc test","price":"1"}]

My Problem its with the link, the output its like this:

http:\/\/catalinaseaspa.com\/wp-content\/uploads\/2015\/03\/island-girl.jpg

But its to be like this:

http://catalinaseaspa.com/wp-content/uploads/2015/03/island-girl.jpg

How can i fix that? Thank you :D

user3000019
  • 103
  • 1
  • 10
  • 1
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jul 31 '15 at 16:38
  • @JayBlanchard I don't think this is the same. That's about parameters, this question is about output. – Barmar Jul 31 '15 at 16:43
  • Why are the backslashes a problem? They'll go away when you parse the JSON. – Barmar Jul 31 '15 at 16:43
  • how are your using encoded data ? json_encode or javascript should be able to get the url right, isn't it ? – Jigar Jul 31 '15 at 16:48
  • Hey jay thanks for the help, i will learn more about prepared statements! Barmar, its a problem because i will need to use the data inside android, and i think i need to have the correct backslashes. Thank you guys!! – user3000019 Jul 31 '15 at 16:49

1 Answers1

0

try this code. Add the JSON_UNESCAPED_SLASHES to the function.

<?php
    mysql_connect("127.0.0.1","root","");
    mysql_select_db("spadramatico_db");

    $query = mysql_query("SELECT * FROM feed_table ORDER BY id");
    $records = array();

    while($obj = mysql_fetch_object($query)) {
        $records [] = $obj;
    }
    print (json_encode($records,JSON_UNESCAPED_SLASHES));

?>
Mohan S Gopal
  • 233
  • 3
  • 12
  • Why should the OP "try this"? A good answer will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO. – Jay Blanchard Jul 31 '15 at 16:47
  • Heey mohan gopal, that fixed the problem, i didnt know about the "JSON_UNESCAPED_SLASHES", thanks for that!!, where can i learn more about json_encode??? – user3000019 Jul 31 '15 at 16:50