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

5
  • 1
    If you can, you should stop using mysql_* functions. They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and consider using PDO, it's really not hard. Commented Jul 31, 2015 at 16:38
  • @JayBlanchard I don't think this is the same. That's about parameters, this question is about output. Commented Jul 31, 2015 at 16:43
  • Why are the backslashes a problem? They'll go away when you parse the JSON. Commented Jul 31, 2015 at 16:43
  • how are your using encoded data ? json_encode or javascript should be able to get the url right, isn't it ? Commented Jul 31, 2015 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!! Commented Jul 31, 2015 at 16:49

1 Answer 1

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)); ?> 
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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???

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.