demo.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) 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把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

如何利用GitLab进行项目文档管理一、背景介绍在软件开发过程中,项目文档是非常重要的资料,不仅能够帮助开发团队了解项目的需求和设计,还能提供给测试团队和客户参考。为了方便项目文档的版本控制和团队协作,我们可以利用GitLab来进行项目文档管理。GitLab是一个基于Git的版本控制系统,除了支持代码管理,还可以管理项目文档。二、GitLab环境搭建首先,我

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6
Visual web development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
