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:
Can someone help me? Thank you!