Home » Php » send array to MySQL in PHP

send array to MySQL in PHP

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m a beginner to PHP. I have a text file and i want to insert file data to the database(MySQL) through PHP. So, I have created an array. As I thought ‘foreach’ is better for it. but it has occurred an error.

<?php
    $servername = "localhost:3308";
    $username = "root";
    $password = '';
    $database = 'naita';

    $conn = new mysqli($servername, $username, $password , $database);

    if($conn->connect_error){
        die("Connection failed: " . $conn->connect_error);
    }else{
        echo "Connection successfully";
        echo "<br>";
    }

    $myfile = fopen("myfile.txt","r") or die("Unable to open file!");
    $data = array();

    while(!feof($myfile)){
        $cha = fgetc($myfile);
        if($cha == '@'){
            echo "<br>";

        }else{
            if($cha == '#'){
                echo "<br>";
            }
            else{
                $cha = fgets($myfile);
                array_push($data,$cha);
            }
        }
    }
    print_r($data);
    foreach ($data as $strs){`send data to mysql`
        $query = "INSERT INTO exam_question(question_title, Option1, Option2, Option3, Option4, answer_options) VALUES ('', '".$strs."')";
        mysql_query($query, $conn);`undefine function`
    }
fclose($myfile);
?> 

It has occurred some error, ‘call undefine function’. How I define it?

How to&Answers: