这是执行 .sql 文件并运行 .sql 文件中编写的所有查询的函数
<?php
//Import (executes) the SQL passed as parameter making some proper adaptations.
function dbImportSQL($sql, $needle = '')
{
$sql = str_replace('/*TABLE_PREFIX*/', DB_TABLE_PREFIX, $sql);
$sentences = explode( $needle . ';', $sql);
// PREPARE THE QUERIES
$var_l = count($sentences);
$s_temp = '';
for($var_k=0;$var_k<$var_l;$var_k++) {
$s = $s_temp.$sentences[$var_k];
if(!empty($s) && trim($s)!='') {
$s .= $needle;
$simple_comma = substr_count($s, "'");
$scaped_simple_comma = substr_count($s, "\'");
if(($simple_comma-$scaped_simple_comma)%2==0) {
$sentences[$var_k] = $s;
$s_temp = '';
//echo "[OK] ".$s." <br />";
} else {
unset($sentences[$var_k]);
$s_temp = $s.";";
//echo "[FAIL] ".$s." <br />";
}
} else {
unset($sentences[$var_k]);
}
}
foreach($sentences as $s) {
$s = trim($s);
if( !empty($s) ) {
$s = trim($s);// . $needle;
if( $this->db->query($s) ) {
$this->debug($s);
} else {
$this->debug($s . ' | ' . $this->db->error . ' (' . $this->db->errno . ')', false);
}
}
}
$this->db_errno = $this->db->errno;
if ($this->db_errno != 0) return false;
return true;
}
?>
注意:请替换$this->db
为您的数据库对象
然后在您创建函数后,现在使用以下代码调用它
$path = 'struct.sql';// define the path for the .sql file
$sql = file_get_contents($path);
dbImportSQL($sql);//this will read all the queries from the sql file and execute them
下面是示例 sql(struct.sql) 文件,如果需要,您可以根据您的确切需要在函数中进行更改
CREATE TABLE IF NOT EXISTS /*TABLE_PREFIX*/t_cpviewer_plan_headers (
`sid` int(11) NOT NULL AUTO_INCREMENT,
`cp` text NOT NULL,
`name` text NOT NULL,
`address` text NOT NULL,
`pages` int(11) NOT NULL DEFAULT '0',
`uploaded` date NOT NULL,
`status` varchar(255) NOT NULL,
PRIMARY KEY (`sid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS /*TABLE_PREFIX*/t_cpviewer_plan_levels (
`sid` int(10) unsigned NOT NULL AUTO_INCREMENT,
`CP` text NOT NULL,
`image` varchar(255) NOT NULL,
`page` int(4) NOT NULL,
`level` int(3) NOT NULL,
`label` text NOT NULL,
PRIMARY KEY (`sid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
ALTER TABLE /*TABLE_PREFIX*/t_item ADD cp_number VARCHAR( 255 ) NOT NULL AFTER dt_expiration ,
ADD cp_level INT( 11 ) NOT NULL AFTER cp_number;