PHP如何在MySQLi数据库中插入JSON对象/数组

时间:2017-04-30 04:27:02

标签: php arrays json object mysqli

我有一个带有数百个键的json对象。这些都作为JSON对象存储在数组中,如下所示:

$allRows= array(
    /* ===== Task ===== */
    "Major task 1" => array(
        "major-task" => array(
            "points" => 1,
            "hours" => 1,
        ),
        "minor-task" => array(
            array(
                "points" => 4,
                "task" => "Minor task 1",
            ),
            array(
                "points" => 2,
                "task" => "Minor task 2",
            ),
            array(
                "points" => 2,
                "task" => "Minor task 3",
            ),
        )
    ),
    /* ===== Task ===== */
    "Major task 2" => array(
        "major-task" => array(
            "points" => 6,
            "hours" => 7,
        ),
        "minor-task" => array(
            array(
                "points" => 4,
                "task" => "Minor task 1",
            ),
            array(
                "points" => 1,
                "task" => "Minor task 2",
            ),
            array(
                "points" => 1,
                "task" => "Minor task 3",
            ),
        )
    ),
);

此对象中的数据并非都是相同的格式,即某些Major tasks可能有也可能没有minor task字段等,因此循环访问对象并插入每个对象是不可行的手动(这似乎也没必要)。

我正在尝试的当前方法:

$conn = connectToDatabase();

// Query for updating the password
$t = time();
$encodedObj = json_encode($allRows);

$statement = "INSERT INTO user_rows(username, sheet, last_update) VALUES ('$user', $encodedObj, $t)";

if ($query = $conn->prepare($statement)) {
    $query->execute();
}
else {
    $error = $conn->errno . ' ' . $conn->error;
    echo "<br><br>" . $error;
    die();
}

我希望能够将json数组/对象直接插入到我的数据库中,以便将它存储在一列中。

感谢任何帮助!

修改:使用bind_param更新了代码:

    $t = time();
    $s = json_encode($allRows);

    $statement = "INSERT INTO user_spreadsheet (username, spreadsheet, last_update) VALUES (:u, :s, :t)";

    if ($query = $conn->prepare($statement)) {
        $query->bind_param(':u', $user);
        $query->bind_param(':s', $s);
        $query->bind_param(':t', $t);
        $query->execute();
    }
    else {
        $error = $conn->errno . ' ' . $conn->error;
        echo "<br><br>" . $error;
        die();
    }

<小时/> 编辑2:我也试过这段代码。这会产生 no 错误,并且查询执行时没有任何问题,但数据实际上并未插入到表中:

    $t = time();
    $s = json_encode($allRows);

    $statement = "INSERT INTO user_spreadsheet (username, spreadsheet, last_update) VALUES (?, ?, ?)";

    if ($query = $conn->prepare($statement)) {
        $query->bind_param('sbi', $user, $s, $t);
        // $query->bind_param(':u', $user);
        // $query->bind_param(':s', $s);
        // $query->bind_param(':t', $t);
        $query->execute();
    }
    else {
        $error = $conn->errno . ' ' . $conn->error;
        echo "<br><br>" . $error;
        die();
    }
    echo "worked";
    return;

1 个答案:

答案 0 :(得分:2)

经过一段时间挖掘代码后,我找到了答案。代码块:

$t = time();
$s = json_encode($allRows);

$statement = "INSERT INTO user_spreadsheet (username, spreadsheet, last_update) VALUES (?, ?, ?)";

if ($query = $conn->prepare($statement)) {
    $query->bind_param('sbi', $user, $s, $t);
    // $query->bind_param(':u', $user);
    // $query->bind_param(':s', $s);
    // $query->bind_param(':t', $t);
    $query->execute();
}
else {
    $error = $conn->errno . ' ' . $conn->error;
    echo "<br><br>" . $error;
    die();
}
echo "worked";
return;

99%正确无误。但有一条线需要改变:

$query->bind_param('sbi', $user, $s, $t);

需要成为

$query->bind_param('ssi', $user, $s, $t);

因为JSON在编码时是一个字符串,需要像这样插入到数据库中。