Home > Backend Development > PHP Tutorial > PHP creates and writes sql database files into the library

PHP creates and writes sql database files into the library

不言
Release: 2023-03-24 15:02:02
Original
2512 people have browsed it

这篇文章介绍的内容是关于PHP创建写入sql数据库文件到库中,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

/导入数据表
        $sqldata=file_get_contents(APP_PATH . 'install/data/mysql.sql');
        $sqlFormat = $this->sql_split($sqldata, $dbpre);//$dbpre为数据表前缀如:yy_ 、tp_等
        //创建写入sql数据库文件到库中 结束
Copy after login


/**
         * 执行SQL语句
         */
        $counts = count($sqlFormat);
        $n = intval($n);
        for ($i = 0; $i < $counts; $i++) {
            $sql = trim($sqlFormat[$i]);
            if (strstr($sql, &#39;CREATE TABLE&#39;)) {
                preg_match(&#39;/CREATE TABLE `([^ ]*)`/&#39;, $sql, $matches);
                mysqli_query($conn,"DROP TABLE IF EXISTS `$matches[1]");
                $ret = mysqli_query($conn,$sql);
                if ($ret) {
                    $message = &#39;<li><span class="correct_span">√</span>创建数据表&#39; . $matches[1] . &#39;,完成!<span style="float: right;">&#39;.date(&#39;Y-m-d H:i:s&#39;).&#39;</span></li> &#39;;
                } else {
                    $message = &#39;<li><span class="correct_span error_span">√</span>创建数据表&#39; . $matches[1] . &#39;,失败!<span style="float: right;">&#39;.date(&#39;Y-m-d H:i:s&#39;).&#39;</span></li>&#39;;
                }
            } else {
                if(trim($sql) == &#39;&#39;)
                    continue;
                $ret = mysqli_query($conn,$sql);
              
            }
        }
Copy after login



function sql_split($sql, $tablepre) {
    if ($tablepre != "yy_")
        $sql = str_replace("yy_", $tablepre, $sql);

    $sql = preg_replace("/TYPE=(InnoDB|MyISAM|MEMORY)( DEFAULT CHARSET=[^; ]+)?/", "ENGINE=\\1 DEFAULT CHARSET=utf8", $sql);

    $sql = str_replace("\r", "\n", $sql);
    $ret = array();
    $num = 0;
    $queriesarray = explode(";\n", trim($sql));
    unset($sql);
    foreach ($queriesarray as $query) {
        $ret[$num] = &#39;&#39;;
        $queries = explode("\n", trim($query));
        $queries = array_filter($queries);
        foreach ($queries as $query) {
            $str1 = substr($query, 0, 1);
            if ($str1 != &#39;#&#39; && $str1 != &#39;-&#39;)
                $ret[$num] .= $query;
        }
        $num++;
    }
    return $ret;
}
Copy after login

相关推荐:

php创建微信公众号管理系统

PHP创建水印



The above is the detailed content of PHP creates and writes sql database files into the library. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template