DEV Community

Cover image for How To Import SQL File With PHP

How To Import SQL File With PHP

Erhan Kılıç on August 26, 2018

This post was first published on How To Import SQL File With PHP Sometimes there are times when you can not use PhpMyAdmin or any database program...
Collapse
 
defman profile image
Sergey Kislyakov

mysql_ functions are deprecated and they have been removed at some point iirc. Use PDO and if your hosting does not support that, you should change the hosting.

Collapse
 
erhankilic profile image
Erhan Kılıç

That's right! Thanks for reminding.

Collapse
 
jakegore profile image
Jake Gore

Could you just use the mysql command like this?

mysql -u username -p database_name < file.sql
Collapse
 
erhankilic profile image
Erhan Kılıç

Yes, you can do that way and it's better and simpler but I wrote this article for who uses shared hosting or free hosting.
You even can't know when you need this. You may need this to help a friend or to do a client's job at a time you've never thought of.

Collapse
 
jakegore profile image
Jake Gore

Ah yeah, I hadn't thought of that. Nice article anyway!

Thread Thread
 
erhankilic profile image
Erhan Kılıç

Thanks! :)

Collapse
 
prezine profile image
Precious Tom

Or you can simply $conn->multi_query(file_get_contents('db.sql'))

Collapse
 
joelde123 profile image
helpyuhboy

worked flawless with PDO 👍