Json encode UTF-8 error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • neelsfer
    Contributor
    • Oct 2010
    • 547

    Json encode UTF-8 error

    I get this JSON create file error called : "Malformed UTF-8 characters, possibly incorrectly encoded", if i try and make a file consisting of about 40 000 records.This happens at record 16669. If i limit the records to less than 16668, it is correct.The record in question is correct, i checked.
    Any suggestions please?
    Code:
    <?php 
    function get_data()
    {
    $connect = mysqli_connect("localhost","root","","registerdb");
    $query = "SELECT * FROM users LIMIT 16700";
    $result = mysqli_query($connect, $query);
    $userdetails = array();
    while($row = mysqli_fetch_array($result))
    {
    $userdetails['userdetails'][] = array(
       'id' => $row["id"],
       'idno' => $row["idno"],
       'surname' => $row["surname"],
       'firstname' => $row["firstname"]
    );
    }
    return json_encode($userdetails);
    }
    
    $file_name = 'userdetails' . '.json';
    
    if(file_put_contents($file_name, get_data()))
    {
    echo $file_name . ' file created';
    }
    else 
    {
    echo json_last_error_msg();	
    echo 'there is an error';
    
    }
    ?>
  • neelsfer
    Contributor
    • Oct 2010
    • 547

    #2
    Anybody else with this issue - I got a fix on google like this
    Code:
    <?php
    $con = mysqli_connect("localhost","root","","registerdb");
    
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
      mysqli_set_charset($con,"utf8");
    ?>

    Comment

    Working...