0

I want to create the database from .sql file but I do not know how to do it. Is it possible to do that?

file connect database PDO

    <?php 
    require "db.sql";
    $dsn = "mysql:host=localhost;dbname=DBNAME;charset=utf8";
    $user= 'root';
    $pass= '';

    try {
        $connect = new PDO($dsn, $user, $pass);
        $connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $connect->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

    } catch (PDOException $e) {

    echo " error ,not connected data base  :".$e->getMessage();

    }

file db.sql

CREATE DATABASE IF NOT EXISTS DBNAME;

CREATE TABLE `nameTable` (
  `id` int(11) NOT NULL AUTO_INCREMENT ,
  PRIMARY KEY (`id`),
  `pseudo` varchar(35) NOT NULL,
  `password` varchar(255) NOT NULL,
  `nameWebsite` varchar(255) NOT NULL DEFAULT 'xxxx'

) ENGINE=InnoDB DEFAULT CHARSET=utf8;


INSERT INTO `nameTable` (`id`, `pseudo`, `password`) VALUES
(1, 'admin', 'admin');
halfer
  • 19,824
  • 17
  • 99
  • 186
jone
  • 11
  • 5
  • no on mysql database phpmyadmin – jone Sep 30 '18 at 11:54
  • 3
    _Note:_ phpmyadmin is just a web based application to manage your mysql database. It has nothing to do with your application. – M. Eriksson Sep 30 '18 at 11:57
  • I do not know how to explain the problem, but I think a simple problem, I want to give a project to a user and I want to create the database from php. i do not want user entered on phpmyadmin and create the database, i want to do it automatically by php – jone Sep 30 '18 at 12:00
  • 3
    Possible duplicate of [Loading .sql files from within PHP](https://stackoverflow.com/questions/147821/loading-sql-files-from-within-php) – halfer Sep 30 '18 at 12:41

0 Answers0