5

I am trying to fetch data from mysql into php and returning it in json format to an controller(angular).

While json creation, some unwanted string is getting appended because of which i am getting error while traversing the json.

Following is my php code:

$json_response = array();

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $row_array["name"] = $row["name"];
    $row_array["quantity"] = $row["quantity"];
    array_push($json_response,$row_array);
}
echo json_encode($json_response);

And following is the console output after printing the json(console is in controller) :

{itemData:{"data":[{"name":"item1","quantity":"10"},{"name":"item2","quantity":"20"},{"name":"item3","quantity":"25"}]      

<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->

}}

Above the highlighted part is coming in the json, because of which the error is happening.

Please help me to resolve the issue.

3
  • What error do you get? Commented Apr 25, 2015 at 8:27
  • Can I suggest you to stop using mysql library? It's deprecated. Move to mysli or better to PDO. This will make your code even safier! Commented Apr 25, 2015 at 8:27
  • I believe it's something to do with your host. The appended code is a script for tracking that seems to be created by your host. I believe that they add it to every request from the server. Try to use exit(); after the echo json_. In case that doesn't help - try using a different host or contact them. Commented Apr 25, 2015 at 8:36

3 Answers 3

4

Ok, After having a little research I can confirmed that my comment is right. Your code is fine and the json string that is being created by your code is alright.

However, the unwanted appended string:

<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->

Is being appended by your server. I believe that it's a free server and therefore they allow themselves to inject their own code into your website. They have a mechanism that append that script into every request that is being made from your server (including ajax requests).

Since this code is being added by the server, it seems that there's nothing you can do in your code to overcome it. It seems that the only option is to look for another hosting company or having a paid plan.

Update - You can try and use the exit(); function right after printing the json string. It might break the host's injection.

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

2 Comments

your solution seems plausible, but i doubt that the exit() works. I believe that the OP's PHP script is being executed in an controlled environment and when that script is terminated, the environment adds the message on it before sending it back to HTTP response.
@KarelG, I suggest that possible solution based on other comments and discussions I've found, otherwise I wouldn't mention it. Of course that the best solution would be to get a paid hosting plan from a decent hosting company.
0

Where $conn is your mysqli connection object: -

    $query = "SELECT  name, quantity FROM tablename";

    if ($result = mysqli_query($conn, $query)) 
    {
        $out = array();

        while ($row = $result->fetch_assoc() )
        {
            $out[] = $row;
        }

        echo json_encode($out);
        mysqli_free_result($result);

    }

Comments

0

To forge your JSON, you can use the little library Simple JSON for PHP.

include('includes/json.php');

$Json = new json();

$Json->add('status', '200');
$Json->add('message', 'Success');
$Json->add("data",$json_response);

$Json->send();

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.