0

我有这个 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);
4

3 回答 3

0

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

例如:

$array = ('key1' => 'value1', 'key2' => 'value2');
于 2012-06-08T16:32:50.437 回答
0

您合并了两段独立的代码,结果一团糟,结果不清楚。

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

$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;
于 2012-06-08T16:37:42.300 回答
0

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

    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";
   }
   }
  ?>
于 2012-06-08T16:47:05.180 回答