RE: [PHP-DB] mysql <database> < ****.sql thru a php script
| From: | John W. Holmes | Date: | Sun, 23 Mar 2003 21:56:28 +0000 |
| Subject: | RE: [PHP-DB] mysql <database> < ****.sql thru a php script | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-26575@lists.php.net to get a copy of this message | ||
> I have a large sql file that I would like to load into mysql database
from
> a
> remote client. Is there a way through a php script to do this?
>
> I have tried $query = " <contents of database.sql> "; (where the
contents
> of
> the sql file was pasted into the query)
>
> then $results = mysql_db_query("database", $query);
>
> but it fails. Any ideas or experience?
You can only run one query at a time. If it is a really large file,
you'll probably want to make an exec() call to have mysql load the file
itself.
$result =
mysql -uroot -ppassword database_name < filename.sql
Those are backticks, not single quotes. $result should be empty if
everything went well. Adapt to your needs.
Or, here's part of a little script I wrote that'll read and execute
*.sql files (from MySQLDump). The str_replace() adds in a table prefix.
This works with my script using ADOdb, so adapt to your needs.
$sql_file = 'survey2.inc';
$file = file($sql_file);
foreach($file as $line)
{
if($line{0} != '#' && strlen($line) > 0)
{
$query .= trim($line);
if(substr($query,-1) == ";")
{
$query =
str_replace('%PREFIX%',$survey->CONF['db_tbl_prefix'],$query);
$query = substr($query,0,-1);
$rs = $survey->db->Execute($query);
if($rs === FALSE)
{
$error = TRUE;
echo '<br><br>' . $survey->db->ErrorMsg();
}
$query = '';
}
}
}
---John W. Holmes...
PHP Architect - A monthly magazine for PHP Professionals. Get your copy
today. http://www.phparch.com/