PHP数据库sql文件 php的sql语句
如何利用PHP执行.SQL文件
代码如下:
创新互联公司坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站制作、网站设计、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的天津网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
?php
class DBManager
{
var $dbHost = '';
var $dbUser = '';
var $dbPassword = '';
var $dbSchema = '';
var $conn;
function __construct($host,$user,$password,$schema)
{
$this-dbHost = $host;
$this-dbUser = $user;
$this-dbPassword = $password;
$this-dbSchema = $schema;
}
public function executeFromString($sql,$delimiter = '(;\n)|((;\r\n))|(;\r)',$prefix = '',$commenter = array('#','--'))
{
return $this-execute($sql,$delimiter,$prefix ,$commenter);
}
public function executeFromFile($sqlPath,$delimiter = '(;\n)|((;\r\n))|(;\r)',$prefix = '',$commenter = array('#','--'))
{
//判断文件是否存在
if(!file_exists($sqlPath))return false;
$handle = fopen($sqlPath,'rb');
$sqlStr = fread($handle,filesize($sqlPath));
fclose($handle);
return $this-execute($sqlStr,$delimiter,$prefix ,$commenter);
}
protected function execute($sqlStr,$delimiter = '(;\n)|((;\r\n))|(;\r)',$prefix = '',$commenter = array('#','--'))
{
//通过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;
endif;
endforeach;
//如果不是注释,则认为是sql语句
if(!$isComment)
$newStatement[] = $subSentence;
endif;
endforeach;
$statement = $newStatement;
endforeach;
//对表名加前缀
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(emptyempty($tableName['leftWall'])):
$newTableName = $prefix.$tableName['name'];
else:
$newTableName = $tableName['leftWall'].$prefix.substr($tableName['name'],1);
endif;
$statement[0] = str_replace($tableName['name'],$newTableName,$statement[0]);
endforeach;
endif;
//组合sql语句
foreach($segment as $statement):
$newStmt = '';
foreach($statement as $sentence):
$newStmt = $newStmt.trim($sentence)."\n";
endforeach;
$statement = $newStmt;
endforeach;
self::saveByQuery($segment);
return true;
}
private function saveByQuery($sqlArray)
{
$this-conn = mysql_connect($this-dbHost,$this-dbUser,$this-dbPassword);
mysql_select_db($this-dbSchema,$this-conn);
foreach($sqlArray as $sql):
mysql_query($sql,$this-conn);
endforeach;
}
public function close()
{
mysql_close($this-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};
endif;
return true;
else:
return self::findTableName($v,$tokens,$tokensKey+1,$tableName);
endif;
endif;
endforeach;
endif;
return false;
}
}
php读取sql文件导入数据库
这样写就不会错了,看看能否帮到你
form id="form1" name="form1" method="post" action="" enctype="multipart/form-data"
input type="file" name="textfield" id="textfield" /
input type="submit" name="button" id="button" value="提交" /
/form
?
if($_POST['button']!=""){
$url=$_FILES['textfield']['tmp_name'];//获取上传来的文件地址
$sql=file_get_contents($url);//打开文件
sql($sql);//执行读取函数
}
function sql($sql){
$conn=mysql_pconnect("localhost","root","123456");
mysql_select_db("shop",$conn);
mysql_query("set names utf8");
$R=mysql_query($sql);
while($v=mysql_fetch_array($R)){
echo $v['ai_id'];
echo "hr";
}
}
//sql($sql);
?
sql文件怎么用php导入到数据库
?php
$file_name = "d:test.sql";
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "123456";
$dbname = "test";
set_time_limit(0);
$fp = @fopen($file_name,"r") or die("sql文件打不开");//打开文件
$pdo = new PDO("mysql:host=localhost;dbname=test","root","123456");//连接数据库
$pdo-query('set names utf8');//设置编码
echo "正在执行导入操作";
while($SQL = GetNextSQL()){
if(!$pdo-query($SQL)){
echo "执行出错";
echo "SQL语句为".$SQL;
}
}
echo "导入完成";
fclose($fp) or die("can't close file");//关闭文件
mysql_close();
//从文件中逐条取sql
function GetNextSQL(){
global $fp;
$sql="";
while($line = @fgets($fp,40960)){
$line = trim($line);
$line = str_replace("////", "//", $line);
$line = str_replace("/","'",$line);
$line = str_replace("//r//n","chr(13).chr(10)",$line);
$line = stripcslashes($line);
if(strlen($line)1){
if($line[0]=='-' $line[1]=="-"){
continue;
}
}
$sql .= $line.chr(13).chr(10);
if(strlen($line)0){
if($line[strlen($line)-1]==";"){
break;
}
}
}
return $sql;
}
亲测有效。。
php 无法创建数据库也不能执行SQL文件
代码比较长,看了后,提几点建议:
既然是要查代码为什么没有达到预期的效果,那么可以将代码各步骤进行分解开。
从代码的功能来看,应该是个建库的代码,那么,要完成一个建库的功能,可能的步骤错误发生之处:
1. 数据库连接不对(用户名、密码错误,没有建库权限等)。
2. 既然是用读取 SQL 文件的形式载入,那么:
a. SQL语句没有编写正确。
b. 文本的编码格式不符(比如ANSI / UTF-8)。
c. 读取SQL后,进行语句处理不正确。
可能还有其他存在错误之处,比如,PHP环境没有设置好、Mysql没有安装配置好等等。
建议你设定断点,然后逐句跟踪调试,这个代码都在一个文件里,跟踪调试比较方便 。
新闻标题:PHP数据库sql文件 php的sql语句
文章网址:http://cdiso.cn/article/hgcidi.html