使用PHP从具有关系的MySQL导出到JSON(对于CouchDB)



我正在写我的硕士论文,我的目标之一是对CouchDB数据库运行测试和实验,并调整性能。

要做到这一点,我需要一些测试数据。我创建了一段php代码来为MySQL数据库生成一些简单的关系数据。表格如下:
客户
产品
品牌
颜色
结账

例如,我在Product和Colored以及Brandid之间建立了一些关系,在Checkout表中,我有一个与Customerid和Productid的关系。

我想将这个完整的数据结构及其关系导出为CouchDB的JSON格式。因此,我有一个JSON字符串,它应该包含每个客户及其属性和购买情况,以及该产品的所有参数,等等

我想它看起来像:

{
    "customer": {
        "customerid" : "1",
        "firstname" : "somefirstname",
        "lastname" : "somelastname",
        "email" : "my@mail.com",
        "country" : "USA",
        "datecreated" : "11111111111111"
    }
    "purchase" : {
        "purchaseid" : "1",
        "product": {
            "productname" : "mightymouse",
            "productcolor": "blue",
            "productbrand" : "Apple",
            "productprice" : "200",
            "checkoutdate" : "1111111111112"
        }
        "purchaseid" : "2",
        "product": {
            "productname" : "something nice",
            "productcolor": "yellow",
            "productbrand" : "Google",
            "productprice" : "5000",
            "checkoutdate" : "11111111113333"
        }
    }
}

这可能不是我展示的正确的数据结构,但它有点像。

这可以在PHP中完成吗?如果可以,我如何创建这种"CouchDB"就绪的JSON字符串??

如果我没有解释清楚,请告诉我。

谢谢
真诚
-Mestika

PHP有函数json_encode,您可以使用它将任何PHP对象转换为其json表示形式(以及json_edecode将json字符串转换为对象)。因此:

  • 使用PHP数据库函数从数据库中读取数据
  • 创建stdClass:的对象

    $data=新stdClass;

  • 用从数据库读取的属性填充此对象,如下所示:

    $customer=新的stdClass;$customer->customerid="1"。。。$data->customer=$customer;

  • 使用json_Encode对生成的对象进行编码。

相关内容

最新更新