lots of ways to do it... one way I've done it when dealing with large sql files is open a pipe to mysqladmin and dump the SQL into it.
PHP Code:
class Process
{
private $descriptorspec;
private $process;
private $pipes;
public function __construct()
{
$this->descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("file", "/tmp/error-output.txt", "a")
);
}
public function connect($command)
{
$this->process = proc_open($command,
$this->descriptorspec,
$this->pipes);
if (!is_resource($this->process))
return false;
}
public function send($message)
{
fwrite($this->pipes[0], $message);
fflush($this->pipes[0]);
}
public function read()
{
while (!feof($this->pipes[1]))
$response .= fgets($this->pipes[1], 1024);
fflush($this->pipes[1]);
return $response;
}
public function close()
{
fclose($this->pipes[0]);
fclose($this->pipes[1]);
return proc_close($this->process);
}
public function __destruct()
{
unset($this->descriptorspec);
}
}
/********** usage *****/
$process =& new Process;
$process->connect('mysqldump -u testuser -p --no-data testdb');
$process->write('test');
$response = $process->read();
$process->close();
echo nl2br($response);
this works... however the process class is "funny" as I cannot seem to read or wite to a proces after the initial read/writes. Still... works for the task at hand (you could replace the command with a mysql command... i.e. 'mysql -u user -p db < sqlfile.sql' and write your password to it. if you don't have pcntl, you can use popen (www.php.net/popen).
Also, in the most barebones of ways, you could read from the sql file line by line and pass it to mysql_query.
Bookmarks