0

* The json_encode is returning NULL? is NOT the answer. I still receive a NULL when using the json_encode.*

I am very new to PHP, so if you could edit the section with the fixed code, I'd appreciate it.

This is my problem:

When an article under "introtext" contains non breaking lines, it returns NULL. The articles that do not have a non breaking space show up just fine.

This is my question:

How can I get the articles under "introtext" to display properly even if they contain a non breaking space.

Here's the code:

$connection = mysqli_connect($host, $user, $pass);

//Check to see if we can connect to the server
if(!$connection)
{
    die("Database server connection failed.");  

}else{

    //Attempt to select the database
    $dbconnect = mysqli_select_db($connection, $db);

    //Check to see if we could select the database
    if(!$dbconnect)
    {
        die("Unable to connect to the specified database!");

        }else{

        $catID = $_GET['catid'];
        $id = $_GET['id'];
        $rtn = $_GET['rtn'];        

        if($id!=""){
            $query = "SELECT * FROM tcp_content WHERE id=" . $id . "";
        }else{
            $query = "SELECT * FROM tcp_content WHERE catid=" . $catID . " ORDER BY publish_up DESC LIMIT " . $rtn . "";
        }

        $resultset = mysqli_query($connection,$query);
        $records = array();


        //Loop through all records and add them to array
        while($r = mysqli_fetch_assoc($resultset))
        {
            $r['introtext'] =  print_r($r['introtext'],true);
            $records[] = $r;        
        }

        //Output the data as JSON
        echo json_encode($records);
    }


}


?>

here are two links:

This link contains the non breaking space, so you'll notice introtext returns NULL

This link does NOT contain the non breaking space, so you'll notice the article shows

Jake
  • 1
  • 2
  • *it returns* what returns? – sectus Nov 11 '13 at 02:09
  • it displays as NULL instead of showing the content of the article. – Jake Nov 11 '13 at 02:17
  • 1
    Show example of text and example of output – sectus Nov 11 '13 at 02:21
  • Click on the two links I have posted at the very bottom of my original post. You'll see one returns the article (it does not contain any hidden non breaking spaces), the other one (that contains hidden non breaking spaces) gives a "NULL" for "introtext". If I post the text, it just looks like spaces because it's hidden. – Jake Nov 11 '13 at 02:23
  • 1
    possible duplicate of [json\_encode is returning NULL?](http://stackoverflow.com/questions/1972006/json-encode-is-returning-null) – mario Nov 11 '13 at 02:26
  • Mario, I tried that fix, but unless I did it wrong, I still got a NULL. – Jake Nov 11 '13 at 02:32

2 Answers2

0

I found this link json_encode problem

see second answer. Charles is suggesting that use iconv() to remove URL encoded non-breaking space.

Community
  • 1
  • 1
Jason Heo
  • 9,956
  • 2
  • 36
  • 64
0

I finally figured it out and got it working

            $r['introtext'] =  utf8_encode($r['introtext']);

            $r['introtext'] =  str_replace(chr(194).chr(160),' ',$r['introtext']);
            $r['introtext'] =  str_replace(chr(194).chr(147),'"',$r['introtext']);
            $r['introtext'] =  str_replace(chr(194).chr(148),'"',$r['introtext']);
            $r['introtext'] =  str_replace(chr(194).chr(146),"'",$r['introtext']);
            $r['introtext'] =  str_replace(chr(194).chr(145),"'",$r['introtext']);

            $r['introtext'] =   htmlentities($r['introtext'], ENT_QUOTES | ENT_IGNORE, "UTF-8");
            $records = $r;      
Jake
  • 1
  • 2