How can JSON data be imported into a MySQL database using PHP?
To import JSON data into a MySQL database using PHP, you can decode the JSON data into an associative array, establish a connection to the MySQL database, and then insert the data into the database using prepared statements.
<?php
// JSON data to be imported
$jsonData = '{
"name": "John Doe",
"age": 30,
"email": "johndoe@example.com"
}';
// Decode JSON data into an associative array
$data = json_decode($jsonData, true);
// Establish connection to MySQL database
$mysqli = new mysqli('localhost', 'username', 'password', 'database_name');
// Prepare SQL statement
$stmt = $mysqli->prepare("INSERT INTO table_name (name, age, email) VALUES (?, ?, ?)");
$stmt->bind_param('sis', $data['name'], $data['age'], $data['email']);
// Execute the statement
$stmt->execute();
// Close the statement and connection
$stmt->close();
$mysqli->close();
?>