0

I have simple php script that shoud output json, but it dont work

<?php 

require 'connect.php';

$sql = "SELECT * FROM horizont";
$result = $con->query($sql);
$rows = array();
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
             $rows[] = $row;
    } 
} 
echo '<pre>';
var_dump($rows);
echo '</pre>';

echo json_encode($rows);
?>

i get result on var dump , but not json code.

var dump result:

http://prntscr.com/e0ef2c

connect.php

<?php
$con = mysqli_connect("localhost","root","","horizont");

// Check connection
 if (mysqli_connect_errno())
 {
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }
 ?>

Fixed with :

 $con->set_charset("utf8");
18
  • Possibly related: stackoverflow.com/q/19361282/2298301 Commented Jan 26, 2017 at 0:17
  • json_encode returns false on failure; you should check that Commented Jan 26, 2017 at 0:18
  • What did you get on the var_dump? Update the question with the data Commented Jan 26, 2017 at 0:18
  • are you going to access the output with ajax? Commented Jan 26, 2017 at 0:21
  • var dump updated, yes i will access with ajax Commented Jan 26, 2017 at 0:23

1 Answer 1

2

For encoding problem try this:

$con->set_charset("utf8");
Sign up to request clarification or add additional context in comments.

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.