Php imports SQL files. Php import SQL file sqlphpphp import SQL file basic idea 1. open the SQL file and place it in a variable (string type). 2. replace the comments in regular expressions with those in php to import SQL files.
Import SQL files using php
sql php
Basic idea of importing SQL files using php
1. open the SQL file and place it in a variable (string type ).
2. replace the comments ("--" and "/**/") with regular expressions.
3. use explode to split an array and remove spaces in each row
4. use my_query () to execute the SQL statement after connecting to the database
Code
// +------------------------------------------------------------------------------------------
// | Author: longDD
// +------------------------------------------------------------------------------------------
// | There is no true,no evil,no light,there is only power.
// +------------------------------------------------------------------------------------------
// | Description: import sql Dates: 2014-08-07
// +------------------------------------------------------------------------------------------
class ImportSql
{
/** @ Var $ content Database Connection */
protected $connect = null;
/** @ Var $ db database object */
protected $db = null;
/** @ Var $ sqlFile SQL file */
public $sqlFile = "";
/** @ Array @ sqlArr SQL statement array */
public $sqlArr = array();
/**
* Constructor
*
* @ Param string $ host address
* @ Param string $ user username
* @ Param string $ pw password
* @ Param $ db_name database name
* @return void
*/
public function __construct($host, $user, $pw, $db_name)
{
/** Connect to the database */
$this->connect = mysql_connect($host, $user, $pw) or die("Could not connect: " . mysql_error());
/** Select database */
$this->db = mysql_select_db($db_name, $this->connect) or die("Yon can not select the table:" . mysql_error());
}
/**
* Import an SQL file
*
* @ Param string $ url file path
* @ Return true import to return true
*/
public function Import($url)
{
if(!file_exists($url))
{
Exit ("The file does not exist! ");
}
$this->sqlFile = file_get_contents($url);
if (!$this->sqlFile)
{
Exit ("An error occurred while opening the file! ");
}
else
{
$this->GetSqlArr();
if ($this->Runsql())
{
return true;
}
}
}
/**
* Obtain the SQL statement array
*
* @return void
*/
public function GetSqlArr()
{
/** Remove comments */
$str = $this->sqlFile;
$str = preg_replace('/--.*/i', '', $str);
$str = preg_replace('/\/\*.*\*\/(\;)?/i', '', $str);
/** Remove spaces to create an array */
$str = explode(";\n", $str);
foreach ($str as $v)
{
$v = trim($v);
if (empty($v))
{
continue;
}
else
{
$this->sqlArr[] = $v;
}
}
}
/**
* Execute the SQL file
*
* @ Return true: Returns true if execution is successful.
*/
public function RunSql()
{
/** Start transaction */
if (mysql_query('BEGIN'))
{
foreach ($this->sqlArr as $k => $v)
{
if (!mysql_query($v))
{
/** Rollback */
mysql_query('ROLLBACK');
Exit ("SQL statement error:". $ k. "row". mysql_error ());
}
}
/** Submit the transaction */
mysql_query('COMMIT');
return true;
}
else
{
Exit ('the transaction cannot be started! ');
}
}
}
// +------------------------------------------------------------------------------------------
// | End of ImportSql class
// +------------------------------------------------------------------------------------------
/**
* This is a example.
*/
header("Content-type:text/html;charset=utf-8");
$sql = new ReadSql("localhost", "root", "", "log_db");
$rst = $sql->Import("./log_db.sql");
if ($rst)
{
echo "Success!";
}
// +------------------------------------------------------------------------------------------
// | End of file ImportSql.php
// +------------------------------------------------------------------------------------------
// | Location: ./ImportSql.php
// +------------------------------------------------------------------------------------------
-
-
-
-
Using php to import SQL files SQL php to import SQL files Basic idea 1. open the SQL file and put it into a variable (string type) 2. replace the comment with regular expressions...