尝试用PHP读取JSON并用SQL编写它的过程可以分为以下几个步骤:
$jsonData = file_get_contents('data.json');
$data = json_decode($jsonData, true);
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
foreach ($data['users'] as $user) {
$id = $user['id'];
$name = $user['name'];
$sql = "INSERT INTO users (id, name) VALUES ('$id', '$name')";
if ($conn->query($sql) === true) {
echo "Record inserted successfully";
} else {
echo "Error inserting record: " . $conn->error;
}
}
$result = $conn->query($sql);
$conn->close();
综上所述,以上是使用PHP读取JSON并用SQL编写的基本步骤。根据具体的业务需求和JSON数据的结构,可能需要进行更多的数据处理和SQL操作。
领取专属 10元无门槛券
手把手带您无忧上云