0

I'm trying to get a JSON object from a MySQL query, in order to add option elements in a select with JavaScript.

My PHP code is:

$carrera = $_GET['idcarrera'];

require_once("inc/config.php"); 
$pdo = conexionPDO();
$sql = "SELECT  materias.clave, materias.nombre FROM carreras_plan
            inner join carreras_plan_materia on carreras_plan.clave = carreras_plan_materia.clave_carrera
            inner join materias on carreras_plan_materia.clave_materia = materias.clave
            where carreras_plan_materia.clave_carrera like $carrera;";
    $ps = $pdo->prepare($sql);
    $ps->execute();

    $data = $ps->fetchAll(PDO::FETCH_ASSOC);
    $json_data = json_encode($data, true);
    echo $json_data;

If I get the JSON and use console.log, I get the query results in the console. Nevertheless, If I erase echo $json_data;, I get the next error when I try to parse the string to JSON:

Uncaught SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)

I think that occurred because the echo $json_data; returns me only square brackets:

enter image description here

Can someone help me? Thank you!

sticky bit
  • 36,626
  • 12
  • 31
  • 42
Nohemi
  • 13
  • 2
  • make sure $data variable is not empty before you do json_encode($data, true); you can do it this way: print_r($data); – evgpisarchik Sep 12 '18 at 21:01
  • 2
    Is `$carrera` a string? Why aren't you binding the variable in your query? Do some basic error checking in your code. – Qirel Sep 12 '18 at 21:01
  • Can you share the following output? print_r($json_data); – Abdulla Sep 12 '18 at 21:01
  • 2
    This will most likely not solve your issue, but the second parameter for `json_encode` are optional bitmask options and should be an integer value (`true` will be most likely casted to 1 though). – Tschitsch Sep 12 '18 at 21:07
  • You're missing quotes around `$carrera` in the query string. If you were checking the query result you would have seen that it failed to execute. – Nick Sep 12 '18 at 21:16
  • @Nick I get the same result adding the quotes. – Nohemi Sep 12 '18 at 21:33
  • @Tschitsch Yeah, that detail not solve the problem, but thank you for your comment. I watched people using the parameter true, but I really don't understood why. – Nohemi Sep 12 '18 at 21:35
  • 1
    @Nohemi They most likely used it on `json_decode`, which would decode to array instead of object. – Tschitsch Sep 12 '18 at 21:37
  • 1
    Why are you using `LIKE` without any wildcards? You might as well use `WHERE clave_carrera = ...` – Phil Sep 12 '18 at 23:09

1 Answers1

1

You need to be using prepared statements, and you should also be checking the results of your query. How do you know if it failed?

$carrera = $_GET['idcarrera'];

require_once("inc/config.php"); 
$pdo = conexionPDO();
$sql = "SELECT  materias.clave, materias.nombre FROM carreras_plan
        inner join carreras_plan_materia on carreras_plan.clave = carreras_plan_materia.clave_carrera
        inner join materias on carreras_plan_materia.clave_materia = materias.clave
        where carreras_plan_materia.clave_carrera like ?";
if ($ps = $pdo->prepare($sql)) {
    if ($ps->execute([$carrera])) {
        $data = $ps->fetchAll(PDO::FETCH_ASSOC);
    } else {
        $data = ["error"=>"execute failed"];
    }
} else {
    $data = ["error"=>"prepare failed"]
}
$json_data = json_encode($data);
echo $json_data;
Phil
  • 157,677
  • 23
  • 242
  • 245
miken32
  • 42,008
  • 16
  • 111
  • 154
  • Where you are assigning the value `idcarrera` in the prepared statement? I get the error "execute failed", but i think is missing that assignment value. – Nohemi Sep 12 '18 at 23:02
  • Well you're using some custom database code so I can't know how it works. I did neglect to put the argument to `execute()` in an array, which would be a problem with the PDO client. Give it a try maybe. – miken32 Sep 12 '18 at 23:05