Read Json in a for a php query

0

I am making a request getjson to a table in MySQL by means of PHP .

What I need to do is to be able to read the answer with for in javascript to get some data.

I have this query in PHP :

 <?php
    $link= mysql_connect('localhost', 'root')
      or die('No se pudo conectar: ' . mysql_error());
      mysql_select_db('sk_modular_divisiones') or die('No se pudo seleccionar la base de datos');
      $query= "SELECT sk_componente, sk_ancho FROM sk_standar WHERE sk_tipo = 'ALTU-148' AND sk_grupo = 'APISO' AND sk_componente = 'ParaExt'";
      $result = mysql_query($query)
                or die("Ocurrio un error en la consulta SQL");
                mysql_close();
      echo json_encode($result);
      //liberar resultador
      mysql_free_result($result);
      //Cerrar la conexión
      mysql_close($link);
    ?>

and I want to convert it to JSON by means of a getjson

var url="json.php";
$.getJSON(url,function(json){
  // loop through the members here
  $.each(json.dinamica,function(i,dat)
}

and through this to read the data that brought me the json to get a value

for (var j = 0; j < (dinamica.length) - 1; j++) {
  if (dinamica[j].sk_ancho < TPARAL && dinamica[j + 1].sk_ancho >= TPARAL && dinamica[j].sk_componenete == PCENTRAL) {
    resultPajustCentral.push(dinamica[j].sk_ancho);
  }
} 

to put it in a table of HTML

document.getElementById("pac").innerHTML = resultPajustCentral[i];

This is what the JSON of the query PHP should bring me:

var dinamica = [{
    "sk_componenete": "Paral",
    "sk_ancho": 30
}, {
    "sk_componenete": "Paral",
    "sk_ancho": 70
}, {
    "sk_componenete": "Paral",
    "sk_ancho": 120
}, {
    "sk_componenete": "Paral",
    "sk_ancho": 170
}, {
    "sk_componenete": "Paral",
    "sk_ancho": 220
}, {
    "sk_componenete": "Paral",
    "sk_ancho": 270
}, {
    "sk_componenete": "Paral",
    "sk_ancho": 320
}, {
    "sk_componenete": "Paral",
    "sk_ancho": 370
}, {
    "sk_componenete": "Paral",
    "sk_ancho": 420
}, {
    "sk_componenete": "Paral",
    "sk_ancho": 470
}, {
    "sk_componenete": "Paral",
    "sk_ancho": 520
}, {
    "sk_componenete": "Paral",
    "sk_ancho": 570
}, {
    "sk_componenete": "Paral",
    "sk_ancho": 620
}, {
    "sk_componenete": "Paral",
    "sk_ancho": 670
}, {
    "sk_componenete": "Paral",
    "sk_ancho": 720
}];
    
asked by Eduard 08.06.2017 в 20:02
source

2 answers

1

first you have this correction in the php

<?php
        $link= mysql_connect('localhost', 'root')
          or die('No se pudo conectar: ' . mysql_error());
          mysql_select_db('sk_modular_divisiones') or die('No se pudo seleccionar la base de datos');
          $query= "SELECT sk_componente, sk_ancho FROM sk_standar WHERE sk_tipo = 'ALTU-148' AND sk_grupo = 'APISO' AND sk_componente = 'ParaExt'";
          $result = mysql_query($query)
                    or die("Ocurrio un error en la consulta SQL");
                    mysql_close();
       echo json_encode($result,true); <------------AQUI AGREGA EL TRUE
       //liberar resultador
       mysql_free_result($result);
       //Cerrar la conexión
       mysql_close($link);
?>

then you should do the following

var url="json.php";
$.getJSON(url,function(json){
  // loop through the members here
  $.each(json,function(i,dat){
      var sk_componenete= dat['sk_componenete'];
      var sk_ancho = dat['sk_ancho'];
  });
}

This way you can get the information you need and store it where you want it

    
answered by 09.06.2017 / 16:05
source
0

In your code I do not see in which part you make the FETCH of the result, you should do it in the following way:

<?php
$link= mysql_connect('localhost', 'root')
  or die('No se pudo conectar: ' . mysql_error());
  mysql_select_db('sk_modular_divisiones') or die('No se pudo seleccionar la base de datos');
  $query= "SELECT sk_componente, sk_ancho FROM sk_standar WHERE sk_tipo = 'ALTU-148' AND sk_grupo = 'APISO' AND sk_componente = 'ParaExt'";
  $result = mysql_query($query)
            or die("Ocurrio un error en la consulta SQL");
            mysql_close();

  //ARREGLO PARA ALMACENAR EL RESULTADO Y CONVERTIRLO EN JSON
  $array = array(); 
  while( $datos = mysql_fetch_array($resultado) ){
    $array[] = $datos;
  }
  echo json_encode($array);

  //liberar resultador
  mysql_free_result($result);
  //Cerrar la conexión
  mysql_close($link);
?>

Now with this you can get the result in ajax:

$.ajax({
 type:"POST",
 url:"xurl.php",
 data:$('form').serialize(),
 dataType:"JSON",
 success:function(r){
   console.log(r);
 },
 error:function(err){
  console.log(err.responseText);
 }
});
    
answered by 10.06.2017 в 19:37