您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php导入sql文件_PHP教程

2024/2/22 14:27:50发布18次查看
php导入sql文件 php导入sql文件sql php
php导入sql文件基本思路1.打开sql文件,放入一个变量(字符串类型)当中
2.使用正则替换掉当中的注释(“--”与“/**/”)
3.使用explode分割成为一个数组并去除每行的空格
4.链接数据库之后使用my_query()执行sql
代码connect = mysql_connect($host, $user, $pw) or die(could not connect: . mysql_error()); /** 选中数据库 */ $this->db = mysql_select_db($db_name, $this->connect) or die(yon can not select the table: . mysql_error()); } /** * 导入sql文件 * * @param string $url 文件路径 * @return true 导入成返回true */ public function import($url) { if(!file_exists($url)) { exit(文件不存在!); } $this->sqlfile = file_get_contents($url); if (!$this->sqlfile) { exit(打开文件错误!); } else { $this->getsqlarr(); if ($this->runsql()) { return true; } } } /** * 获取sql语句数组 * * @return void */ public function getsqlarr() { /** 去除注释 */ $str = $this->sqlfile; $str = preg_replace('/--.*/i', '', $str); $str = preg_replace('/\/\*.*\*\/(\;)?/i', '', $str); /** 去除空格 创建数组 */ $str = explode(;\n, $str); foreach ($str as $v) { $v = trim($v); if (empty($v)) { continue; } else { $this->sqlarr[] = $v; } } } /** * 执行sql文件 * * @return true 执行成功返回true */ public function runsql() { /** 开启事务 */ if (mysql_query('begin')) { foreach ($this->sqlarr as $k => $v) { if (!mysql_query($v)) { /** 回滚 */ mysql_query('rollback'); exit(sql语句错误:第 . $k . 行 . mysql_error()); } } /** 提交事务 */ mysql_query('commit'); return true; } else { exit('无法开启事务!'); } }}// +------------------------------------------------------------------------------------------// | 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// +------------------------------------------------------------------------------------------
http://www.bkjia.com/phpjc/871202.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/871202.htmltecharticlephp导入sql文件 php导入sql文件 sql php php导入sql文件 基本思路 1.打开sql文件,放入一个变量(字符串类型)当中 2.使用正则替换掉当中的注释...
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product