I have to import a (big) .sql
file to MySQL with PHP and the Zend framework.
The connection is open, and I have $_sql
containing the whole content of the SQL file, but - of course, a simple
$this->db->query($_sql);
will not work.
What can I do to import this large file?
Update: Yes, it is a mysql-dump with structure-definitions of my mysql-database. And yes, it need to run from within php without using command line.
SOLVED: I need a mysqli object to fix that, mysqli->multiQuery is the way to go. Here's what my (unit test) bootstrap look like:
<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'testing'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$options = $application->getOptions();
$_sqlFile = realpath(APPLICATION_PATH.'\\..\\tests\\sql\\');
$_sqlFile .= '\\myLargeSQL.sql';
$sql = file_get_contents($_sqlFile);
$mysqliobject = new mysqli(
$options['resources']['db']['params']['host'],
$options['resources']['db']['params']['username'],
$options['resources']['db']['params']['password'],
$options['resources']['db']['params']['dbname']
);
$mysqliobject->multi_query($sql);
if ($mysqliobject->errno <> 0) {
throw new Exception('Error creating temp database in ('.__FILE__.' on line '.__LINE__.')');
}
$mysqliobject->close();
Don't worry about the \, this is only temporarly (will fix that soon) and yes, i am on a windows machine. So, maybe somebody can use it.