D
D
Develm2015-12-12 16:45:43
PHP
Develm, 2015-12-12 16:45:43

How to extract array from mysql json?

There is a code

$(".send-id-form").submit(function(){
      var data = {
        "action": "test"
      };
      data = $(this).serialize() + "&" + $.param(data);
      $.ajax({
        type: "POST",
        dataType: "json",
        url: "../modules/configurate/json.php", //Relative or absolute path to response.php file
        data: data,
        success: function(data) {
        $(".test-ajax-js").html(
          "Favorite beverage: " + data["Name"] + "<br />Favorite restaurant: " + data["Descrpition"] + "<br />Gender: " + data["id"]
        );

        console.log("Form submitted successfully.\nReturned json: " + data["json"]);
        }
      });
      return false;
      });

and php
if (is_ajax()) {
  if (isset($_POST["action"]) && !empty($_POST["action"])) { //Checks if action value exists
    $action = $_POST["action"];
    switch($action) { //Switch case for value of action
      case "test": test_function(); break;
    }
  }
}

//Function to check if the request is an AJAX request
function is_ajax() {
  return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}

function test_function(){
  $id = $_POST['id_edit_now'];
  $data = array();
  $send = mysql_query("SELECT * FROM structure WHERE id = '$id'");
  
  while($row = mysql_fetch_assoc($send)){ 								  
    $data[] = $row; 
  }
  
  echo json_encode($data);
}

In general, I can’t catch up with how to display it like in php mysql_fetch_assoc, only json - data["here is the field name"]

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Stalker_RED, 2015-12-12
@Stalker_RED

If you only need one field, do SELECT myField FROM ...insteadSELECT *

isset($_POST["action"]) && !empty($_POST["action"])
- redundant construction, it is enough !empty($_POST["action"])in empty() to have its own built-in isset().
And yes, they tell you everything about injections and pdo correctly.

A
Alexander Litvinenko, 2015-12-12
@edli007


php.net/manual/en/function.mysql-query.php
Warning
This extension has been deprecated since PHP 5.5.0 and will be removed in the future.
php.net/manual/en/ref.pdo-mysql.php

A
Anton Shcherbakov, 2015-12-12
@WestTrade

You are selecting all columns in a table. If you want to display only the necessary data, use the construct:
$send = mysql_query("SELECT `FIELD` FROM structure WHERE id = '$id'");
$data = $row['FIELD'];
or
$data = $row['0'];

A
Alexander Taratin, 2015-12-12
@Taraflex


$id = $_POST['id_edit_now'];
$data = array();
$send = mysql_query("SELECT * FROM structure WHERE id = '$id'");
https://www.google.ru/webhp?sourceid=chrome-instan...
phpfaq.ru/pdo

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question