skip to Main Content

How correctly run sql file for insert in MySQL?
Try use PDO:

$mysql_host = "localhost";
$mysql_database = "planets_test";
$mysql_user = "root";
$mysql_password = "";
# MySQL with PDO_MYSQL
$dbh= new PDO("mysql:host=$mysql_host;dbname=$mysql_database", $mysql_user, $mysql_password);
$query = file_get_contents("planets_db.sql");
$stmt = $dbh->prepare($query);
$stmt->execute();

Where planets_db.sql” is script generated PHPmyAdmin?

Script not return error, but table not created.

2

Answers


  1. Since this is a script you can execute SQL with the following code:

    try {
         $mysql_host = "localhost";
         $mysql_database = "planets_test";
         $mysql_user = "root";
         $mysql_password = "";
         $dbh= new PDO("mysql:host=$mysql_host;dbname=$mysql_database", $mysql_user, $mysql_password);
         $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
         $query = file_get_contents("planets_db.sql");
         $dbh->exec($query);
         print("Created $table Table.n");
    
    } catch(PDOException $e) {
        echo $e->getMessage();//Remove or change message in production code
    }
    

    If there is an error it should tell you what went wrong.

    Login or Signup to reply.
  2. I checked your code in my PC. Nothing found any error. Please check your SQL file in the same directory or is it correct SQL file.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search