使用PHP将JSON数据插入MySQL

时间:2018-04-27 18:39:59

标签: php mysql json

我试图将它插入MySQL Base,但它对我不起作用,请帮助解决这个问题。 这就是JSON的样子:

{
    "status": 1,
    "response": {
        "Aviator Goggles": {
        "price": 1009,
        "quantity": 269
    },
    "Aviator Sunglasses": {
        "price": 460,
        "quantity": 187
    },
    "BIKER CRATE": {
        "price": 29,
        "quantity": 3569
    }
 },
    "time": 1524852778
}

我的PHP代码解析并插入数据:

<?php
$jsonurl = "https://api.opskins.com/IPricing/GetAllLowestListPrices/v1/?appid=578080&format=json_pretty";
$json = file_get_contents($jsonurl);
$data = json_decode($json, true);
print_r ($data);

mysql_connect("127.0.0.1", "root", "") or die (mysql_error ());
mysql_select_db("pubg") or die(mysql_error());

foreach($data as $item) {

       mysql_query("INSERT INTO `c5f` (response, price, quantity) 
       VALUES ('".$item['response']."','".$item['price']."','".$item['quantity']."')") or die(mysql_error());
     }
?>

3 个答案:

答案 0 :(得分:0)

使用PDO将数据插入数据库;这是一些示例代码:

// Set these to your login data
define('DB_HOST', '127.0.0.1');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'pubg');

// We connect to the database using the values above
$pdo = new PDO('mysql:host='. DB_HOST .';dbname='. DB_NAME, DB_USER, DB_PASS);
// We tell PDO to report us every error
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// Now we prepare a query
$sql = $pdo->prepare('INSERT INTO `c5f` SET `response` = :response, `price` = :price, `quantity` = :quantity;');

// We can use $sql to insert data
$data = $response['data'];
foreach($data as $key => $item)
    $sql->execute(array(
        ':response' => $key,
        ':price'    => $item['price'],
        ':quantity' => $item['quantity']
    ));
    echo 'Insert: '. htmlentities($key) .' ('. $item['quantity'] .' - '. $item['price'] .')'."\r\n";
}

这将连接到数据库,准备插入语句并为每个数据集执行它。

答案 1 :(得分:0)

如果你看一下解码的json(print_r($data)),你会看到:

Array
(
    [status] => 1
    [response] => Array
        (
            [Aviator Goggles] => Array
                (
                    [price] => 1009
                    [quantity] => 269
                )

            [Aviator Sunglasses] => Array
                (
                    [price] => 460
                    [quantity] => 187
                )

            [BIKER CRATE] => Array
                (
                    [price] => 29
                    [quantity] => 3569
                )

        )

    [time] => 1524852778
)

要进行数据库插入(我在这里复制你的mysql调用,但你真的应该升级,至少要升级到MySQLI,但在我看来PDO更好,并且使用{{3} }},你需要遍历$data['response']

foreach ($data['response'] as $key => $item) {
    mysql_query("INSERT INTO `c5f` (response, price, quantity) 
        VALUES ('$key','{$item['price']}','{$item['quantity']}')") or die(mysql_error());
}

答案 2 :(得分:-1)

因为您的查询类似于...VALUES("response")

但您的内部文字包含"字符

它会像...VALUES("{ "price": 460, "quantity": 187 },")

一样疯狂

你应该使用mysql_real_escape_string函数来逃避所有内部文本:

"... VALUES ('".mysql_real_escape_string($item['response'])."', ..."

玩得开心