note 65478 added to ref.mysql
| From: | sb at stephenbrooks dot org | Date: | Mon, 01 May 2006 20:43:52 +0000 |
| Subject: | note 65478 added to ref.mysql | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-110288@lists.php.net to get a copy of this message | ||
I'm in the process of changing web hosts and my previous host provided a "dump" of
the database in the form of a sequence of SQL requests that (I assume) have to be executed in order
to rebuild the database on another system. It was generated using "MySQL dump 9.11".
Queries are finished by a semicolon and linefeed, while comment-lines begin with a double hyphen.
The script below opens a connection to an SQL server and loads a dump file $file of this format into
the database $dest_db.
function load_db_dump($file,$sqlserver,$user,$pass,$dest_db)
{
$sql=mysql_connect($sqlserver,$user,$pass);
mysql_select_db($dest_db);
$a=file($file);
foreach ($a as $n => $l) if (substr($l,0,2)=='--') unset($a[$n]);
$a=explode(";\n",implode("\n",$a));
unset($a[count($a)-1]);
foreach ($a as $q) if ($q)
if (!mysql_query($q)) {echo "Fail on '$q'"; mysql_close($sql); return 0;}
mysql_close($sql);
return 1;
}
This may not be watertight if the ";\n" sequence appears inside queries, but I hope it
helps others who are in posession of such dumps.
----
Server IP: 66.163.161.117
Probable Submitter: 81.179.94.83
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/ref.mysql.php
Edit -- http://master.php.net/note/edit/65478
Del: integrated -- http://master.php.net/note/delete/65478/integrated
Del: useless -- http://master.php.net/note/delete/65478/useless
Del: bad code -- http://master.php.net/note/delete/65478/bad+code
Del: spam -- http://master.php.net/note/delete/65478/spam
Del: non-english -- http://master.php.net/note/delete/65478/non-english
Del: in docs -- http://master.php.net/note/delete/65478/in+docs
Del: other reasons-- http://master.php.net/note/delete/65478
Reject -- http://master.php.net/note/reject/65478
Search -- http://master.php.net/manage/user-notes.php