-1

I want to import data from sql dump generated by phpMyAdmin programmatically using php.

I found some questions but they did not helped me because i don't have shell access and there is no problems due to foreign-key constraints.

Create tables from SQL dump generated by phpmyadmin using kohana

Programmatic Equivalent Of Import in phpMyAdmin

----EDIT----

This is code i am using, it works fine but i think it breaks when there is some html in database.

sqldump_to_statements($contents)
{

    // remove c style and inline comments with -- and #
    $comment_patterns = array('/\/\*.*(\n)*.*(\*\/)?/','/\s*--.*\n/','/\s*#.*\n/');
    $contents = preg_replace($comment_patterns, "\n", $contents);
    $statements = explode(";\n", $contents);
    $statements = preg_replace("/\s/", ' ', $statements);
    return $statements;
}
Community
  • 1
  • 1
Imran Naqvi
  • 2,202
  • 5
  • 26
  • 53
  • 1
    Just to make sure that I have it right, are you saying that you have made a dump file from PHPMyAdmin and you would like your own PHP script to be able to take the file and then process it to re-populate a database? – Marc Towler Jun 25 '11 at 19:57
  • Sorry if i was not able to explain myself, @Marc is exactly got it the right way. Thanks @Marc. – Imran Naqvi Jun 25 '11 at 20:11
  • Sorry for inconvenience due to unexplained question statement. I just edited it hope that works that time. – Imran Naqvi Jun 25 '11 at 20:21
  • you shouldnt need to remove the comments if you followed what i said for when it comes to exporting your SQL file from PHPMyAdmin – Marc Towler Jun 25 '11 at 20:41

3 Answers3

1

The issue with answering the question in it's current format is that there is two interpretations that could be made.

  1. You are asking how to import the file that you generate via PHPMyAdmin, to do this all you need to do is to go to the copy of PHPMyAdmin that is connected to the database that you would like to use. Once you have logged in, select the database to be used then near the top of the page you have a tab marked import, just click on that and upload the file as requested and sit back for PHPMyAdmin to do it's job
  2. You are asking us how you can write a PHP script to take the exported SQL file and process it, kind of like an installation script... When you export the SQL file you need to make sure that you do not select any compression setting and that you export it either as text or an SQL file so that it is easier to process, it would also be better if you told the export script to not include comments, this is so that you can avoid physically needing to edit the file if you don't want to. From here you have two choices, you can either manually split the file into parts, i.e. one file for all of the table creation queries, a different file for all the INSERT queries. The second choice here is to process the whole file as it is using PHP queries, you can include the file as whole in your script then possibly look at splitting the file into an array to process easier and to let the end user know how the process is going.

Edit: I know it is wordy and if you needed it to help I could look at providing some code examples

Marc Towler
  • 705
  • 11
  • 32
  • +1, Thanks @Marc for your response. Your second option is what i need. I will try to export the dump using your guidelines and probably will update the code also. – Imran Naqvi Jun 25 '11 at 20:26
0

I am not sure whether this is what you want, but you could just use phpMyAdmin again to import the dump. It would be good to get some additional information.

Select the database you want to import into. Select the Import tab. And from there you can select your file and define other parameters.

Hope this helps.

cwoebker
  • 3,158
  • 5
  • 27
  • 43
  • Thanks @cwoebker for your response but my question clearly stats that i want to do it using my own php script problematically not manually using phpmyadmin. – Imran Naqvi Jun 25 '11 at 20:13
-1

Using phpMyAdmin:

1: Make sure that the database you are importing into is created and wiped clean, however if you are just restoring your database remember to drop all your tables. You can use the DROP option from the dropdown box at the bottom of your tables list combined with all selected tables.

2: Select you database

3: Click the Import link from the top menu at the top of your screen. You will see a Text Box area and a file upload option.

4: Click browse on the file upload option and find your .sql file. Then click the ‘GO’ button to start the process.

Note: If you encounter any problem while uploading, it’s most likely because of the size of you .sql file as the importing size is very limited, don’t worry there is a very few changes that you need to add to your php.ini and it should be ok after.

1- Locate your php.ini configuration file and change the values of upload_max_filesize, memory_limit and post_max_size to larger sizes than your.sql file’s one.

2- Restart your apache server and try again.

There is a second method using MySQL client GUI wich a lot easier please visit this link for the complete detail http://www.alfasky.com/2008/04/how-to-backup-and-restore-a-mysql-database/