" /> 成人短视频在线观看免费,在线观看网址你懂的,a一级日本特黄aaa大片

一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

如何利用PHP執行.SQL文件

demo.php:
復制代碼 代碼如下:
<?php
/**
* 讀取 sql 文件并寫入數據庫
* @version 1.01 demo.php
*/
class DBManager
{
    var $dbHost = '';
    var $dbUser = '';
    var $dbPassword = '';
    var $dbSchema = '';

    function __construct($host,$user,$password,$schema)
    {
        $this->dbHost = $host;
        $this->dbUser = $user;
        $this->dbPassword = $password;
        $this->dbSchema = $schema;
    }

    function createFromFile($sqlPath,$delimiter = '(;/n)|((;/r/n))|(;/r)',$prefix = '',$commenter = array('#','--'))
    {
        //判斷文件是否存在
        if(!file_exists($sqlPath))
            return false;

        $handle = fopen($sqlPath,'rb');  

        $sqlStr = fread($handle,filesize($sqlPath));

        //通過sql語法的語句分割符進行分割
        $segment = explode(";",trim($sqlStr));

        //var_dump($segment);

        //去掉注釋和多余的空行
        foreach($segment as & $statement)
        {
            $sentence = explode("/n",$statement);

            $newStatement = array();

            foreach($sentence as $subSentence)
            {
                if('' != trim($subSentence))
                {
                    //判斷是會否是注釋
                    $isComment = false;
                    foreach($commenter as $comer)
                    {
                        if(eregi("^(".$comer.")",trim($subSentence)))
                        {
                            $isComment = true;
                            break;
                        }
                    }
                    //如果不是注釋,則認為是sql語句
                    if(!$isComment)
                        $newStatement[] = $subSentence;                  
                }
            }

            $statement = $newStatement;
        }
        //對表名加前綴
        if('' != $prefix)
        {

      
            //只有表名在第一行出現時才有效 例如 CREATE TABLE talbeName

            $regxTable = "^[/`/'/"]{0,1}[/_a-zA-Z]+[/_a-zA-Z0-9]*[/`/'/"]{0,1}$";//處理表名的正則表達式
            $regxLeftWall = "^[/`/'/"]{1}";

            $sqlFlagTree = array(
                    "CREATE" => array(
                            "TABLE" => array(
                                    "$regxTable" => 0
                                )
                        ),
                    "INSERT" => array(
                            "INTO" => array(
                                "$regxTable" => 0
                            )
                        )

                    );

            foreach($segment as & $statement)
            {
                $tokens = split(" ",$statement[0]);

                $tableName = array();
                $this->findTableName($sqlFlagTree,$tokens,0,$tableName);

                if(empty($tableName['leftWall']))
                {
                    $newTableName = $prefix.$tableName['name'];
                }
                else{
                    $newTableName = $tableName['leftWall'].$prefix.substr($tableName['name'],1);
                }

                $statement[0] = str_replace($tableName['name'],$newTableName,$statement[0]);
            }

        }      
        //組合sql語句
        foreach($segment as & $statement)
        {
            $newStmt = '';
            foreach($statement as $sentence)
            {
                $newStmt = $newStmt.trim($sentence)."/n";
            }

            $statement = $newStmt;
        }

        //用于測試------------------------      
        //var_dump($segment);
        //writeArrayToFile('data.txt',$segment);
        //-------------------------------

        self::saveByQuery($segment);

        return true;
    }

    private function saveByQuery($sqlArray)
    {
        $conn = mysql_connect($this->dbHost,$this->dbUser,$this->dbPassword);

        mysql_select_db($this->dbSchema);

        foreach($sqlArray as $sql)
        {
            mysql_query($sql);
        }      
        mysql_close($conn);
    }

    private function findTableName($sqlFlagTree,$tokens,$tokensKey=0,& $tableName = array())
    {
        $regxLeftWall = "^[/`/'/"]{1}";

        if(count($tokens)<=$tokensKey)
            return false;      

        if('' == trim($tokens[$tokensKey]))
        {
            return self::findTableName($sqlFlagTree,$tokens,$tokensKey+1,$tableName);
        }
        else
        {
            foreach($sqlFlagTree as $flag => $v)
            {  
                if(eregi($flag,$tokens[$tokensKey]))
                {
                    if(0==$v)
                    {
                        $tableName['name'] = $tokens[$tokensKey];

                        if(eregi($regxLeftWall,$tableName['name']))
                        {
                            $tableName['leftWall'] = $tableName['name']{0};
                        }

                        return true;
                    }
                    else{
                        return self::findTableName($v,$tokens,$tokensKey+1,& $tableName);
                    }
                }
            }
        }

        return false;
    }
}
function writeArrayToFile($fileName,$dataArray,$delimiter="/r/n")
{
    $handle=fopen($fileName, "wb");

    $text = '';

    foreach($dataArray as $data)
    {
        $text = $text.$data.$delimiter;
    }
    fwrite($handle,$text);
}
//測試
$dbM = new DBManager('localhost','w01f','123456','test');
$dbM->createFromFile('data.sql',null,'fff_');
?>

data.sql:
-- phpMyAdmin SQL Dump
-- version 2.11.3
-- http://www.phpmyadmin.NET
--
-- 主機: localhost
-- 生成日期: 2008 年 08 月 20 日 12:09
-- 服務器版本: 5.0.51
-- php 版本: 5.2.5
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- 數據庫: `newysh`
--
-- --------------------------------------------------------
--
-- 表的結構 `allowed`
--
CREATE TABLE `allowed` (
`bhash` blob NOT NULL,
`bname` varchar(255) character set utf8 NOT NULL,
PRIMARY KEY (`bhash`(20))
) ENGINE=MyISAM DEFAULT CHARSET=gb2312 ROW_FORMAT=DYNAMIC;
--
-- 導出表中的數據 `allowed`
--
-- --------------------------------------------------------
--
-- 表的結構 `allowed_ex`
--
CREATE TABLE `allowed_ex` (
`bhash` blob NOT NULL,
`badded` datetime NOT NULL,
`bsize` bigint(20) unsigned NOT NULL,
`bfiles` int(10) unsigned NOT NULL,
PRIMARY KEY (`bhash`(20))
) ENGINE=MyISAM DEFAULT CHARSET=gb2312 ROW_FORMAT=DYNAMIC;
--
-- 導出表中的數據 `allowed_ex`
--
-- --------------------------------------------------------
--
-- 表的結構 `category`
--
CREATE TABLE `category` (
`cid` int(10) unsigned NOT NULL auto_increment COMMENT '種子分類id',
`name` varchar(255) NOT NULL COMMENT '分類名稱,支持html格式',
`sequence` int(10) unsigned NOT NULL COMMENT '顯示排序,需要小的排在前面',
PRIMARY KEY (`cid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=26 ;
--
-- 導出表中的數據 `category`
--
INSERT INTO `category` (`cid`, `name`, `sequence`) VALUES
(25, '音樂', 23),
(24, '學習資料', 24),
(23, '電影', 25);
-----------------------------------------------------------
注:對于phpmyadmin 生成的sql文件均適用

php技術如何利用PHP執行.SQL文件,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 在线免费一区 | 国产男女免费完整视频 | 色婷婷久久综合中文久久一本 | 福利精品 | 欧美日韩另类在线观看视频 | 国产精品视频一区二区三区不卡 | 亚洲伊人成人 | 一级成人a毛片免费播放 | 成人福利网站 | 国产精品久久久久久久成人午夜 | 欧美黄色大片免费观看 | 欧美视频a | 91视频一88av| 亚洲国产一级a毛片 | 91高清免费视频 | 91在线亚洲 | 久久青青91费线频观青 | 韩国特级一级毛片免费网站 | 婷婷色爱区综合五月激情韩国 | 成人免费观看黄a大片夜月 成人免费观看视频 | 亚洲视频第一页 | 美女胸又大又www又黄的网站 | 在线精品国产一区二区 | 永久免费视频 | 日本一区免费在线 | 91人人视频 | 国模私拍福利一区二区 | 91久久精品国产亚洲 | 在线观看91精品国产入口 | 最大胆极品欧美人体 | 日本人真淫视频一区二区三区 | 九月丁香十月婷婷在线观看 | 六月婷婷在线视频 | 成人精品视频一区二区在线 | 中文婷婷 | 韩国免费乱理论片在线观看2018 | 国产精品第一页在线观看 | 中文字幕一区二区三区 精品 | 91久久国产综合精品女同国语 | 色精品一区二区三区 | 另类国产精品一区二区 |