How can JSON data stored in a MySQL database be decoded and output as an array in PHP?
To decode JSON data stored in a MySQL database and output it as an array in PHP, you can retrieve the JSON data from the database, decode it using the `json_decode()` function, and then access the decoded data as an array. You can then iterate through the array to display or manipulate the data as needed.
// Connect to MySQL database
$mysqli = new mysqli("localhost", "username", "password", "database");
// Retrieve JSON data from database
$result = $mysqli->query("SELECT json_data FROM table_name WHERE id = 1");
$row = $result->fetch_assoc();
$json_data = $row['json_data'];
// Decode JSON data into array
$data_array = json_decode($json_data, true);
// Output the array
print_r($data_array);
Keywords
Related Questions
- What are the recommended alternatives to using the mysql_* functions in PHP for database operations like updating records based on form inputs?
- What are the best practices for structuring and grouping data extracted from a CSV file using PHP arrays?
- What are the advantages of using associative names for CSV column headers in PHP compared to numerical indexes?