Ans: In this tutorial we will learn how to store multidimensional array in mysql database using php.

Consider, we have the below questions array, In which we have the multiple choice questions.
$questions = array(
    1 => array(
        'Question' => 'What is HTML stands for',
        'Answers' => array(
            'A' => 'Hyper Text Markup Language',
            'B' => 'Hybrid Text Markup Language',
            'C' => 'Homepage Text Markup Language'
        ),
        'CorrectAnswer' => 'A'
    ),
    2 => array(
        'Question' => 'Second question',
        'Answers' => array(
            'A' => 'First answer of Second question',
            'B' => 'Second answer Second question',
            'C' => 'Third answer Second question'
        ),
        'CorrectAnswer' => 'C'
    )
);
First, we need to encode the array data into json using php function json_encode(). json_encode() function is used to encode the array into json and json_decode() is used to convert the json data into php array.

Create questions table in mysql:
=======================
Below is the structure of questions table with basic fields.
CREATE TABLE `questions` (
`id` INT NOT NULL AUTO_INCREMENT,
`que` TEXT NULL,
PRIMARY KEY (`id`));

Create Connection(config.php)
=======================
Connect to the database. In config.php add your database name, database username, database password and database name.
<?php
//Database details
$db_host = 'localhost';
$db_username = 'username';
$db_password = 'password';
$db_name = 'database_name';

//Create connection and select DB
$conn = mysqli_connect($db_host, $db_username, $db_password, $db_name);
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

insert.php
========
<?php
require_once 'config.php';

$questions = array(
    1 => array(
        'Question' => 'What is HTML stands for',
        'Answers' => array(
            'A' => 'Hyper Text Markup Language',
            'B' => 'Hybrid Text Markup Language',
            'C' => 'Homepage Text Markup Language'
        ),
        'CorrectAnswer' => 'A'
    ),
    2 => array(
        'Question' => 'Second question',
        'Answers' => array(
            'A' => 'First answer of Second question',
            'B' => 'Second answer Second question',
            'C' => 'Third answer Second question'
        ),
        'CorrectAnswer' => 'C'
    )
);
$json_ques = json_encode($questions);
mysqli_query($conn, "insert into questions(que) values ('$json_ques')");
echo "Record inserted successfully.";
?>

Written by
Check out this Author's contributed articles.
If you like Tutorialsmint and would like to contribute, you can also write an article using Write an Article. See your article appearing on the Tutorialsmint main page and help other programmers to enhance their knowledge.

Comments(0)

RECENT TUTORIALS
Wed Dec 26 • 08:00 AM GMT
Wed Dec 26 • 08:00 AM GMT
Wed Dec 26 • 08:00 AM GMT
Wed Dec 26 • 08:00 AM GMT
Wed Dec 26 • 08:00 AM GMT
Edit Post
Add Reply