1

This may be obvious, but I have exported a database via phpmyadmin to a .sql file. I'm trying to import the file into another database via a php script, but I can't figure out how to do this. I'm sure it's easy to do since i can easily accomplish the same thing by running the query through phpmyadmin. Any suggestions?

2

5 Answers 5

7

You can run mysql command using system() function of php.

You can not do with mysql_query as this function can not run multiple queries

system('mysql -u <user> -p<password> dbname < filename.sql');
Sign up to request clarification or add additional context in comments.

Comments

3

I wrote this code and it worked for me. Hope it helps.

Note: It seems a bit too simple, could anyone, please, warn me if I'm overlooking something? Thanks

$connection = new mysqli(DB_SERVER, DB_USER, DB_PASS, $databaseName);

$query = "";

foreach ($sqlFileContents as $line) {
  $line = trim($line);

  if ($line != "" && substr($line, 0, 2) != '--') {
    $query .= $line;

    if (substr($line, -1) == ';') {
      $connection->query($query);

      echo ($query."\n");

      if ($connection->errno) {
        echo ("\n".$connection->errno . ": ");
        echo ($connection->error."\n");
      }

      $query = "";
    }
  }
}

unset ($line);

if ($connection->errno) {
  echo ($connection->errno . ": ");
  echo ($connection->error);
}

$connection->close();

Comments

1

A very nice answer:

Best practice: Import mySQL file in PHP; split queries

all you need to add here is an upload function

Comments

1

If it's a giant SQL file, all you need to do is

$sql = file_get_contents('sqlfile.sql');
//make the connection
mysql_query($sql); //assuming what you have is actually valid SQL 
Just curious, why not just do this through a DB utility?

EDIT : As Shakthi Singh points out, you can't run multiple queries in mysql_query in one go (not tested though).

However, Depending on your file this might still cause problems as talked about in this question : PHP: multiple SQL queries in one mysql_query statement (Some statments not being supported by mysql_query)

So depending on how your SQL looks the above solution might no be viable.

All is not lost however, this thread : Loading .sql files from within PHP talks about a few alternatives. User arin sarkissian points out that mysqli_multi_query() is an option

1 Comment

@Shakti Singh good point. I've clarified my answer to take this into account
0

Read in the file, then

mysql_query(file_contents)

Beware the timeoutwock, my son.

3 Comments

@Shakti Singh: How do SQL injection attacks against PHP using ; to terminate a query and start a new one work then? Bobby Tables would have no effect on a web site using the mighty mysql_query...
I'm not talking about SQL injection here. As stated in manual mysql_query() sends a unique query (multiple queries are not supported). OR you could try by yourself if it works.
@Shakti Singh: just tried it. Worked fine; only got results for the first query, but the later ones executed. I think the documentation is either for a newer version of PHP or full of it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.