• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to import a sql file in codeigniter?

The only difference between doing this in native PHP and doing this in CodeIgniter is that you can use CI's database library to send the individual queries to the database. So, you would probably use file_get_contents() to read the .sql file into a string. In a fairly simple case, you would just explode the string on a semicolon (';') to split up the commands in the .sql file so you can execute them. Then you loop over the array of queries generated by exploding the string, trim each query, and pass it to db->query().

If you want to get complicated, you can do the whole thing in a transaction and roll it back if any of the queries fail. You could also attempt to parse the string for comments and anything else you might want to remove.

It would also be highly recommended that you check the SQL for any commands you don't want executed against your database, and that you ensure the ability to run this code is very well protected from execution by unauthorized users.

Messages In This Thread
RE: How to import a sql file in codeigniter? - by mwhitney - 10-27-2015, 09:41 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.