How to Import Database using Codeigniter

Here I have developed an application in Codeigniter which will help you to take import as per your wish wnenever you want to take import. While most web hosting company do a daily import of a customer’s database and site, relying on them to make restore and provide them at no cost is risky also.

Method 1

public function import_database() 
{
	$path = 'assets/database_backup/';
	$sql_filename = 'crmdata.sql';

	$sql_contents = file_get_contents($path.$sql_filename);
	$sql_contents = explode(";", $sql_contents);

	foreach($sql_contents as $query)
	{
		$pos = strpos($query,'ci_sessions');
		var_dump($pos);
		if($pos == false)
		{
			$result = $this->db->query($query);
		}
		else 
		{
			continue;
		}
	}
}

Method 2

public function import_database() 
{
	$templine = '';
	$lines = file($_FILES["uploadFile"]["tmp_name"]); 
	foreach ($lines as $line)
	{
		// Skip it if it's a comment
		if (substr($line, 0, 2) == '--' || $line == '')
		continue;

		// Add this line to the current templine we are creating
		$templine .= $line;

		// If it has a semicolon at the end, it's the end of the query so can process this templine
		if (substr(trim($line), -1, 1) == ';')
		{
		// Perform the query
		$this->db->query($templine);

		// Reset temp variable to empty
		$templine = '';
		}
	}
	$this->session->set_flashdata('SUCCESSMSG', "Database Import Successfully!!");
	redirect('setting/download_database');
}

Leave a Reply

Your email address will not be published. Required fields are marked *