RE: mysql <database> < ****.sql thru a php script

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> 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/



-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux