Import .sql file via PHP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jamie Meyers

    Import .sql file via PHP

    Does anyone know how to import a .sql dump file into a mysql database using
    php. I know how to do it using commandline, mysql < test.sql, but I am
    writing an install script, and was wondering if it is possible to do it this
    way. If not, do you know of any parser that will do all the importing/table
    creation the .sql file defines? Any response is much appreciated.

    Thanks,
    Jamie


  • Stephen Harris

    #2
    Re: Import .sql file via PHP


    "Jamie Meyers" <gtg061q@mail.g atech.edu> wrote in message
    news:d7e8ge$26g $1@news-int2.gatech.edu ...[color=blue]
    > Does anyone know how to import a .sql dump file into a mysql database
    > using php. I know how to do it using commandline, mysql < test.sql, but I
    > am writing an install script, and was wondering if it is possible to do it
    > this way. If not, do you know of any parser that will do all the
    > importing/table creation the .sql file defines? Any response is much
    > appreciated.
    >
    > Thanks,
    > Jamie[/color]

    I saw these instructions in the install text for PBS, an AMP program.

    "Now we put data in our fresh database. In directory pbs/dump is a dump with
    example data. Use mysql dbname < pbssample_engli sh.dmp.
    (dbname is $db_name in environment.php 3)"

    I think the dump delivered database structure for a help desk program,
    PBS. I don't know much about this so you got "any response".

    Another example:
    a..
    1.. go to the directory where the command mysql is recognized and type the
    commands displayed in bold below:
    a.. prompt$ mysql
    b.. mysql> create database allonto;
    c.. mysql> quit
    d.. prompt$ mysql allonto < allonto.dmp

    1.. Run the following command to dump your Mysql database:
    2.. mysqldump -f -t -n >bacula-backup.dmp>


    a..


    Comment

    • Jamie Meyers

      #3
      Re: Import .sql file via PHP

      Nevermind, I decided to parse the .sql file and create the query there. I
      do not think there is any way to import the .sql file via php. Thanks
      anyways. And if anyone would like this script after I fix it up a little
      more, let me know.

      Jamie

      "Stephen Harris" <cyberguard10 48-usenet@yahoo.co m> wrote in message
      news:Iuyme.257$ IE7.16@newssvr2 1.news.prodigy. com...[color=blue]
      >
      > I saw these instructions in the install text for PBS, an AMP program.
      >
      > "Now we put data in our fresh database. In directory pbs/dump is a dump
      > with example data. Use mysql dbname < pbssample_engli sh.dmp.
      > (dbname is $db_name in environment.php 3)"
      >
      > I think the dump delivered database structure for a help desk program,
      > PBS. I don't know much about this so you got "any response".
      >
      > Another example:
      > a..
      > 1.. go to the directory where the command mysql is recognized and type
      > the commands displayed in bold below:
      > a.. prompt$ mysql
      > b.. mysql> create database allonto;
      > c.. mysql> quit
      > d.. prompt$ mysql allonto < allonto.dmp
      >
      > 1.. Run the following command to dump your Mysql database:
      > 2.. mysqldump -f -t -n >bacula-backup.dmp>
      >
      >
      > a..
      >
      >[/color]


      Comment

      • Anonymous

        #4
        Re: Import .sql file via PHP

        Jamie Meyers wrote:[color=blue]
        >
        > Does anyone know how to import a .sql dump file into a mysql database using
        > php. I know how to do it using commandline, mysql < test.sql, but I am
        > writing an install script, and was wondering if it is possible to do it this
        > way. If not, do you know of any parser that will do all the importing/table[/color]

        If you know how to do it with a commandline what's wrong with
        exec("mysql < test.sql"); ?

        Comment

        • Tim Van Wassenhove

          #5
          Re: Import .sql file via PHP

          On 2005-05-30, Jamie Meyers <gtg061q@mail.g atech.edu> wrote:[color=blue]
          > Nevermind, I decided to parse the .sql file and create the query there. I
          > do not think there is any way to import the .sql file via php. Thanks
          > anyways. And if anyone would like this script after I fix it up a little
          > more, let me know.[/color]

          Assuming each instruction is on a line.. (untested)

          $db = mysql_connect(. ...);
          mysql_select_db (....);

          $fp = fopen('somefile .sql', 'r');
          while($fp != feof())
          {
          $line = fread($fp, 2048);
          $line = mysql_real_esca pe_string($db, $line);
          mysql_query($li ne);
          }
          fclose($fp);


          --
          Met vriendelijke groeten,
          Tim Van Wassenhove <http://timvw.madoka.be >

          Comment

          • Ivan Omelchenko 608308824

            #6
            Re: Import .sql file via PHP

            Anonymous пишет:[color=blue]
            > Jamie Meyers wrote:
            >[color=green]
            >>Does anyone know how to import a .sql dump file into a mysql database using
            >>php. I know how to do it using commandline, mysql < test.sql, but I am
            >>writing an install script, and was wondering if it is possible to do it this
            >>way. If not, do you know of any parser that will do all the importing/table[/color]
            >
            >
            > If you know how to do it with a commandline what's wrong with
            > exec("mysql < test.sql"); ?[/color]
            can be denied on public hostings!

            Comment

            • Ivan Omelchenko 608308824

              #7
              Re: Import .sql file via PHP

              Tim Van Wassenhove ÐÉÛÅÔ:[color=blue]
              > On 2005-05-30, Jamie Meyers <gtg061q@mail.g atech.edu> wrote:
              >[color=green]
              >>Nevermind, I decided to parse the .sql file and create the query there. I
              >>do not think there is any way to import the .sql file via php. Thanks
              >>anyways. And if anyone would like this script after I fix it up a little
              >>more, let me know.[/color]
              >
              >
              > Assuming each instruction is on a line.. (untested)
              >
              > $db = mysql_connect(. ...);
              > mysql_select_db (....);
              >
              > $fp = fopen('somefile .sql', 'r');
              > while($fp != feof())
              > {
              > $line = fread($fp, 2048);
              > $line = mysql_real_esca pe_string($db, $line);
              > mysql_query($li ne);
              > }
              > fclose($fp);
              >
              >[/color]
              I guess it will NOT working, because of line can be much more than 2048
              bytes, or it can be BLOB or another..
              Or, can be comment multi line. your script will failed.

              Comment

              • Tim Van Wassenhove

                #8
                Re: Import .sql file via PHP

                On 2005-05-30, Ivan Omelchenko 608308824 <news@omelchenk o.com> wrote:[color=blue]
                > Tim Van Wassenhove ÐÉÛÅÔ:[/color]
                [color=blue][color=green]
                >> Assuming each instruction is on a line.. (untested)[/color][/color]
                [color=blue]
                > I guess it will NOT working, because of line can be much more than 2048
                > bytes, or it can be BLOB or another..[/color]

                That is up to the OP to find out what his needs are..

                [color=blue]
                > Or, can be comment multi line. your script will failed.[/color]

                That wouldn't meet the "each instruction is on a line" requirement..


                Meaby a file_get_conten ts and mysqli_multiy_q uery are better suited for
                the task..


                --
                Met vriendelijke groeten,
                Tim Van Wassenhove <http://www.timvw.info>

                Comment

                • Jamie Meyers

                  #9
                  Re: Import .sql file via PHP

                  If you have ever seen a mysql dump, it is just a dump of all the tables, and
                  all the data in the tables, unless specified otherwise. Everything is
                  broken up into multiple lines. A simple while statement worked for me, but
                  i just need to finish the error checking of the script. I will post it when
                  I am done.

                  Jamie


                  Comment

                  Working...