How to correctly fetch a SQL result using MySQL PHP?

217
June 23, 2017, at 2:13 PM

I'm trying to fetch the results from a MySQL query. The results are later saved on an array and encoded as JSON to be used on Ionic2 framework. I have tried a simple SELECT * from query and I still get the same error "Unexpected token < in JSON at position 0"

I suspect my mistake is inside my while loop, though I can't figure out the origin.

PHP:

<?php
header("Access-Control-Allow-Origin:http://localhost:8100");
header("Content-Type: application/x-www-form-urlencoded");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
 $datos;
 $resultados_finales;
 @$db = mysqli_connect("localhost","root","","speedomart");
if($db)
{
        //$query = "SELECT * FROM producto"; 
        $query = "SELECT producto.codigo_barra, producto.nombre, producto.imagen, producto.precio, stock_prod.ubicacion from producto JOIN stock_prod ON stock_prod.idSup = 1 ORDER BY RAND() LIMIT 0,2";
        $data=mysqli_query($db,$query);
        while($fila=mysqli_fetch_assoc($data))
        {
            $codigo = $fila[0];
            $nombre = $fila[1];
            $imagen = $fila[2];
            $precio = $fila[3];
            $ubicacion = $fila[4];

            $resultados_finales = array("mensage"=>"algcorrecto","codigo"=>$codigo,"nombre"=>$nombre,"imagen"=>$imagen,"precio"=>$precio,"ubicacion"=>$ubicacion);
            echo json_encode($resultados_finales);
        }
}else
{
    $resultados_finales = array("mensage"=>"credenciales incorrectas");
    echo json_encode($resultados_finales);
};
?>
Answer 1

I suggest you use mysqli_fetch_array() Function Fetch a result row as a numeric array and as an associative array.

Try this code.

<?php
header("Access-Control-Allow-Origin:http://localhost:8100");
header("Content-Type: application/x-www-form-urlencoded");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
 $datos;
 $resultados_finales;
 @$db = mysqli_connect("localhost","root","","speedomart");
if($db)
{
        //$query = "SELECT * FROM producto"; 
        $query = "SELECT producto.codigo_barra, producto.nombre, producto.imagen, producto.precio, stock_prod.ubicacion from producto JOIN stock_prod ON stock_prod.idSup = 1 ORDER BY RAND() LIMIT 0,2";
        $data=mysqli_query($db,$query);
        while($fila=mysqli_fetch_array($data))
        {
            $codigo = $fila[0];
            $nombre = $fila[1];
            $imagen = $fila[2];
            $precio = $fila[3];
            $ubicacion = $fila[4];

            $resultados_finales[] = array("mensage"=>"algcorrecto","codigo"=>$codigo,"nombre"=>$nombre,"imagen"=>$imagen,"precio"=>$precio,"ubicacion"=>$ubicacion);
        }
  echo json_encode($resultados_finales);
}else
{
    $resultados_finales = array("mensage"=>"credenciales incorrectas");
    echo json_encode($resultados_finales);
};
?>
READ ALSO
how to call json on local folder

how to call json on local folder

How do I call a local JSON file using javascript and php?

253
Fetch array value from previous page and display in a table

Fetch array value from previous page and display in a table

So im trying to figure out how to display the data in table from an array which i get from previous pagebelow is the code is used to retrieve the barang_id value, the code below works fine, i use it to display "barang_id" which i get from previous page;

171
PHP parse json and insert to database

PHP parse json and insert to database

I have a json dataWhat i want is to parse it and insert on database

217