1

Hey I am iOS developer I am trying to create simple JSON output from my website. I found good start link and here is some explanation how to do it.

So I've created accounts.php file and put it to my public_html folder

<?php

include_once("JSON.php");
$json = new Services_JSON();

$link = mysql_pconnect("localhost", "user", "pass") or die("Could not connect");
mysql_select_db("iglobe") or die("Could not select database");

$arr = array();

$rs = mysql_query("SELECT * FROM users");
while($obj = mysql_fetch_object($rs)) {
    $arr[] = $obj;
}

Echo $json->encode($arr);

?>

Of course I use my user and password and I pointed my just created database ob my end.

so when I try to request my file so http//mywebsite.com/accounts.php there is no data.

I tried to use google chrome and Postman so it says No response received when I switch to JSON. For HTML there is no info in Postman.

My question how can I test it? even if I use Echo(123) before include_once("JSON.php"); line there is no 123 on html page.

I tried to test PHP with only this code:

<?php
    phpinfo();
    ?>

and it works. I have PHP Version 5.4.32

5
  • I think your code crashes before it reaches the echo... If you don't see 123 Commented Nov 23, 2015 at 21:20
  • 1
    Turn on all debug options: display_errors and error_reporting. If nothing else, even if the query completely barfed, you should be getting an empty json-encoded array: []. Since you aren't, the script bailed BEFORE the echo statements. Commented Nov 23, 2015 at 21:20
  • Add echo after every command and see where it breaks.. so one before $link and one before $rs Commented Nov 23, 2015 at 21:21
  • 1
    Thats a genuinely bad tutorial. Read the comments Commented Nov 23, 2015 at 21:26
  • 1
    ok if you read my answer (which i deleted) i have to apologize, Echo actually works. Wow, I have to hate php more and more. Commented Nov 23, 2015 at 21:30

2 Answers 2

2

First of all, simply use PHP's function json_encode($arr). It does exactly what you are asking for and is pretty much included in every version of PHP that I can think of.

Documentation

Also, I am not sure if this is the issue, but you may want to change Echo ==> echo. This is generally convention at the very least.

SUPER IMPORTANT

Finally, DO NOT USE mysql extension. Its is dangerous, may not work correctly, and has security vulnerabilities. Use mysqli or PDO.

Sign up to request clarification or add additional context in comments.

Comments

0

Matrosov -

You are very close. Use the json_encode function to output your code via the PHP manual. Also consider using mysqli instead of mysql for your database connection as it has been better support for modern MySQL servers.

http://php.net/manual/en/function.json-encode.php

http://php.net/manual/en/book.mysqli.php

<?php

    include_once("JSON.php");

    $link = mysqli_connect("localhost", "user", "pass") or die("Could not connect");
    $link->mysql_select_db("iglobe") or die("Could not select database");

    $arr = array();

    $rs = mysql_query("SELECT * FROM users");
    while($obj = mysql_fetch_object($rs)) {
        $arr[] = $obj;
    }

    echo json_encode($arr);

    ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.