繁体   English   中英

PHP JSON响应数组

[英]Php JSON Response Array

我有这个PHP代码。 如您所见,我通过showallevents函数查询mysql数据库。 我将$ result返回到$ event变量。 通过while循环,我将从事件中获取的值分配给响应数组,并且每次循环发生时,行都存储在数据数组中。 当然,我在某个地方失败了,因为尽管我得到了正确数量的响应,但我在json上获得的所有值都是“空”。 它还告诉我有关JSONarray的某些信息无法转换为jsonobject

 if (isset($_POST['tag']) && $_POST['tag'] != '') 
 {
// get tag
$tag = $_POST['tag'];

// include db handler
require_once 'include/DB_Functions.php';
$db = new DB_Functions();

// response Array
$response = array("tag" => $tag, "success" => 0, "error" => 0);

// check for tag type
if ($tag == 'showallevents') 
{
    // Request type is show all events
    // show all events
    $event = $db->showallevents();
    if ($event != false)
    {
    $data = array();
    while($row = mysql_fetch_assoc($event)) 
        {
        $response["success"] = 1;
        $response["uid"] = $event["uid"];
        $response["event"]["date"] = $event["date"];
        $response["event"]["hours"] = $event["hours"];
        $response["event"]["store_name"] = $event["store_name"];
        $response["event"]["event_information"] = $event["event_information"];
        $response["event"]["event_type"] = $event["event_type"];
        $response["event"]["Phone"] = $event["Phone"];
        $response["event"]["address"] = $event["address"];
        $response["event"]["created_at"] = $event["created_at"];
        $response["event"]["updated_at"] = $event["updated_at"];
        $data[]=$response;

        }
        echo json_encode($data);            
    }
    else 
    {
        // event not found
        // echo json with error = 1
        $response["error"] = 1;
        $response["error_msg"] = "Events not found";
        echo json_encode($response);
    }
}
  else 
 { 
echo "Access Denied"; 
 }
}
?>

DB_Functions.php

     <?php

    class DB_Functions 
   {

private $db;

//put your code here
// constructor
function __construct() 
{
    require_once 'DB_Connect.php';
    // connecting to database
    $this->db = new DB_Connect();
    $this->db->connect();
}

// destructor
function __destruct()
{

}


/**
 * Select all events that are after yesterday.
 */
public function showallevents()
{
   $result = mysql_query("SELECT * FROM events WHERE date >= CURDATE()");
   return($result);
   }

   }

  ?>

好的帮助我将所有数据放入数组的代码是

            $data = array();
            while($row = mysql_fetch_assoc($event)) 
        {
        $response["success"] = 1;
        $response["event"]= $row;
        $data[]=$response;
        }
        echo json_encode($data);

您的$response变量是什么? 在PHP中创建关联数组,使用=>而不是=

例如:

$array = ('key1' => 'value1', 'key2' => 'value2');

您已经合并了两个独立的代码段,并弄得一团糟,其结果尚不清楚。

您可以通过两种方式创建关联数组:

$array = (key=>value, key2=>value2);

您还可以使用:

$array[key]=value;
$array[key2]=value2;

注意,“键”和“值”都是变量。 您可以在此处使用字符串,也可以从其他地方传入变量。

当做类似的事情时,我使用以下方法:

    $response["success"] = 1;
    $response["uid"] = $event["uid"];
    $response["event"]["date"] = $event["date"];
    $response["event"]["hours"] = $event["hours"];
    $response["event"]["store_name"] = $event["store_name"];
    $response["event"]["event_information"] = $event["event_information"];
    $response["event"]["event_type"] = $event["event_type"];
    $response["event"]["Phone"] = $event["Phone"];
    $response["event"]["address"] = $event["address"];
    $response["event"]["created_at"] = $event["created_at"];
    $response["event"]["updated_at"] = $event["updated_at"];
    $data[]=$response;

如果只想返回所有数组,可以代替mysql_fetch_row使用mysql_fetch_assoc吗?

    if ($tag == 'showallevents') 
{
    // Request type is show all events
    // show all events
    $event = $db->showallevents();
    if ($event != false)
    {
    $data = array();
    while($row = mysql_fetch_assoc($event)) 
        {
        $data[] = $row;
        echo json_encode($data);            
    }
    else 
    {
        // event not found
        // echo json with error = 1
        $response["error"] = 1;
        $response["error_msg"] = "Events not found";
        echo json_encode($response);
    }
}

     else
    {
echo "Access Denied";
   }
   }
  ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM