0

Using a javascript application I am making a server side request for a search functionality. It works for some queries, but not for others.

For some queries the GET request returns no response, despite the fact that testing the query in workbench returns thousands of records. I tried turning on errors - no errors are generated. I tried increasing memory limit - that's not the culprit. I tried to output PDO errors/warnings - nothing generated, the PDO actually returns the records, I verified this using var_dump, as shown below.

So to conclude, everything in the below code, seems to work flawlessly, until the final line that is responsible for encoding the array into a json object - it echos nothing.

I appreciate any assistance in resolving this.

PHP

error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('memory_limit', '2048M');
$db = new PDO('mysql:host=localhost;dbname=mydb', 'root', '');

$search = $_GET['search'];
$searchBy = ( isset($_GET['searchBy']) && !empty($_GET['searchBy']) ) ? $_GET['searchBy'] : 'name';


$sql = "SELECT * FROM business WHERE name LIKE '%university%' GROUP BY id"; 
$query = $db->prepare($sql);
$query->execute();
$results = $query->fetchAll(); //query returns 5000+ records in under a second in workbench

$headers = array('Business', 'City', 'State', 'Zip', 'Phone');


$json = array("results" => $results, 'headers' => $headers, 'query' => $sql);
var_dump($json); //prints successfully 

echo json_encode($json); // echos nothing!
16
  • 1
    What is the exact output of: var_dump(json_encode($json)); ? Commented Apr 26, 2015 at 1:26
  • 1
    Then change it so e.g. if you get: string(7) "[1,2,3]" -> write something like: string(7) "[X,X,X]" Commented Apr 26, 2015 at 1:29
  • 1
    Store it in files for a while so that when it goes wrong you have what actually was generated. Commented Apr 26, 2015 at 1:34
  • 1
    @AnchovyLegend completely blank So you don't even get this: string(0) "" (Also make sure you show into the source code!)? Commented Apr 26, 2015 at 1:36
  • 1
    Always run with something that works! :) Glad it is sorted out. Commented Apr 26, 2015 at 2:30

3 Answers 3

3

EDIT:

use utf8_encode() then json_encode() as mentioned here (Special apostrophe breaks JSON)

OR

$dbHandle = new PDO("mysql:host=$dbHost;dbname=$dbName;charset=utf8", $dbUser, $dbPass,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));

will parse all the data in utf8.

Old

What's the result if you do this ?

echo json_encode($results);
//scratch those
//$json = array("results" => $results, 'headers' => $headers, 'query' => $sql);
//var_dump($json); //prints successfully 

May be you run out of memory? Try with a 100 results. I know from experience with other projects that json_encode can consume a shared server's ram.

Sorry if I am not as helpful as you would like me to be but we can't really test you code, you have to do it for us.

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

Comments

2

I had this exact error when I tried to use json_encode the other day. It returned no errors... just a blank screen, and finally it hit me. The issues was with character encoding. The column I was trying to use had been used by copywriters who cut and pasted from Microsoft Word directly into the wysiwyg.

echo json_last_error() after your json_encode and see what you get

.
.
.
echo json_encode($json); // echos nothing!
echo json_last_error(); // integer if error hopefully 0

It should return an integer specifying one of the errors below.

0 = JSON_ERROR_NONE
1 = JSON_ERROR_DEPTH
2 = JSON_ERROR_STATE_MISMATCH
3 = JSON_ERROR_CTRL_CHAR
4 = JSON_ERROR_SYNTAX
5 = JSON_ERROR_UTF8

In my case it returned a 5. I then had to clean every "description" column before it would work. In the event it is the same error, I've included the function you will need to run all your columns through to clean them out.

function utf8Clean($string)
{
    //reject overly long 2 byte sequences, as well as characters above U+10000 and replace with *
    $string = preg_replace('/[\x00-\x08\x10\x0B\x0C\x0E-\x19\x7F]'.
     '|[\x00-\x7F][\x80-\xBF]+'.
     '|([\xC0\xC1]|[\xF0-\xFF])[\x80-\xBF]*'.
     '|[\xC2-\xDF]((?![\x80-\xBF])|[\x80-\xBF]{2,})'.
     '|[\xE0-\xEF](([\x80-\xBF](?![\x80-\xBF]))|(?![\x80-\xBF]{2})|[\x80-\xBF]{3,})/S',
     '*', $string );

    //reject overly long 3 byte sequences and UTF-16 surrogates and replace with ?
    $string = preg_replace('/\xE0[\x80-\x9F][\x80-\xBF]'.
     '|\xED[\xA0-\xBF][\x80-\xBF]/S','?', $string );

    return $string;
}

for more info on json_last_error() go to the source! http://php.net/manual/en/function.json-last-error.php

Comments

0

It is really easy with the JSON Builder : Simple JSON for PHP

include('includes/json.php');

$Json = new json();

$Json->add('status', '200');
$Json->add('message', 'Success');
$Json->add('query', 'sql');
$Json->add("headers",$headers);
$Json->add("data",$results);

$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.