Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Funky output in json_encode

Tags:

php

mysql

I'm getting some weird output when I json_encode the response from my MySQL database. I'm VERY new to PHP and only working with it for school, any help is appreciated.

Here is the code:

<?php
require_once('./database.php');

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

if (isset($_GET['format'])) {
    $format = filter_var($_GET['format']);
}

if (isset($_GET['action'])) {
    $action = filter_var($_GET['action'], FILTER_SANITIZE_STRING);
    $tableName = "sk_$action";
}

$query = "SELECT * FROM $tableName";

if (isset($_GET['course'])) {
    $course = filter_input(INPUT_GET, 'course');
    $query .= " WHERE courseID = :course_id";
}

$statement = $db->prepare($query);
$statement->bindValue(':course', $course);
$statement->execute();

$response = $statement->fetchAll();

$statement->closeCursor();

echo json_encode($response);

Here is the response:

enter image description here

I don't know where the 0's and 1s are coming from, nor how to get rid of them. Can someone please point me in the right direction?

Ok, so I added this to the code and now the application crashes.

$response = $statement->fetchAll(PDO::FETCH_ASSOC);

$statement->closeCursor();

if ($format == 'json') {
    echo json_encode($response);
}
if ($format == 'xml') {
    $xml = new SimpleXMLElement($response[0]);
    array_walk_recursive($response, array($xml, 'addChild'));
    print $xml->asXML();
}

The JSON part works correctly, but when I have format=xml, the page doesn't load. Got anymore of those good hints guy?

like image 289
MMelvin0581 Avatar asked Mar 05 '19 00:03

MMelvin0581


People also ask

Which function is used to encode a value to JSON format?

The json_encode () function is used to encode a value to JSON format. Required. Specifies the value to be encoded Optional.

What is JSON_encode ()?

Like the reference JSON encoder, json_encode () will generate JSON that is a simple value (that is, neither an object nor an array) if given a string, int, float or bool as an input value. While most decoders will accept these values as valid JSON, some may not, as the specification is ambiguous on this point.

How do you encode a JSON string in jQuery?

If the browser provides a native implementation of JSON.parse (), here jQuery uses to encode the string. “ {hello: 45}”: string ‘hello’ should have double-quotes. “ {‘hello’:89}”: string ‘hello’ should be enclosed in double-quotes but not single quotes.

How to encode JSON from stdClass object of PHP to readable form?

JSON Encode Online to encode JSON from stdClass Object of PHP to readable form. Paste or type your data here.... JSON Encode Online is easy to use tool to encode JSON data, which converts stdClass Object of PHP to JSON. Copy, Paste, and Encode.


2 Answers

Those are due to the default fetch_style of PDOStatement::fetchAll(), which returns both 0-indexed and named columns for your rows.

PDO::FETCH_BOTH (default): returns an array indexed by both column name and 0-indexed column number as returned in your result set

You can use PDO::FETCH_ASSOC or PDO::FETCH_OBJ to capture just the column names (the former producing an associative array keyed with the column names and the latter producing an anonymous object whose properties are the column names):

$response = $statement->fetchAll(PDO::FETCH_ASSOC);
like image 76
Marty Avatar answered Oct 23 '22 13:10

Marty


Change your database fetch_style to an associative one:

If you are using PDO, check out PDO::FETCH_ASSOC

like image 29
Miroslav Glamuzina Avatar answered Oct 23 '22 13:10

Miroslav Glamuzina