下面来看看php 如何导入.sql文件的代码
<?php
if(!defined('WEBROOT')) {
die('Access Denied!');
}
class dbsql {
var $dblink = NULL;
var $queryCount = 0;
function dbconnect($dbserver, $dbuser, $dbpwd, $dbname = '', $pconnect = 0, $charset = 'utf8') {
if($pconnect) {
if(!$this->dblink = @mysql_pconnect($dbserver, $dbuser, $dbpwd)) {
$this->msghalt('Can not connect to MySQL server');
}
} else {
if(!$this->dblink = @mysql_connect($dbserver, $dbuser, $dbpwd)) {
$this->msghalt('Can not connect to MySQL server');
}
}
/* if($this->version() > '4.1') {
global $charset, $dbcharset;
if(!$dbcharset && in_array(strtolower($charset), array('gbk', 'big5', 'utf-8'))) {
$dbcharset = str_replace('-', '', $charset);
}
if($dbcharset) {
@mysql_query("SET character_set_connection=$dbcharset, character_set_results=$dbcharset, character_set_client=binary", $this->dblink);
}
if($this->version() > '5.0.1') {
@mysql_query("SET sql_mode=''", $this->dblink);
}
} */
@mysql_query('set names "utf8"');
if($dbname) {
if(!mysql_select_db($dbname, $this->dblink)) {
$this->msghalt('Can not connect to MySQL server');
}
}
}
function select_db($dbname) {
return @mysql_select_db($dbname, $this->dblink);
}
function query($sql = '') {
if($sql) {
if(!($query = mysql_query($sql,$this->dblink))) {
$this->msghalt("MYSQL Query Error!n $sql");
} else {
$this->queryCount++;
return $query;
}
} else {
return false;
}
}
function unb_query($sql = '') {
if($sql) {
if(!($query = mysql_unbuffered_query($sql,$this->dblink))) {
$this->msghalt('MYSQL Query Error!<br />'.$sql.'<br />');
} else {
$this->queryCount++;
return $query;
}
} else {
return false;
}
}
function fetch_array($query, $result_type = MYSQL_ASSOC) {
return mysql_fetch_array($query, $result_type);
}
function fetch_one($sql) {
return $this->fetch_array($this->query($sql));
}
function affected_rows() {
return mysql_affected_rows($this->dblink);
}
function num_rows($query, $q = 1)
{
if($q == 1) {
return mysql_num_rows($query);
} else {
return mysql_num_rows($this->query($query));
}
//return $query;
}
function result($query, $row) {
$query = @mysql_result($query, $row);
return $query;
}
function num_row($query) {//暂时没用
return mysql_num_rows($query);
}
function free_result($query) {
return mysql_free_result($query);
}
function insert_id() {
return ($id = mysql_insert_id($this->dblink)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"));
}
function num_fields($query) {
return mysql_num_fields($query);
}
function version() {
return mysql_get_server_info($this->dblink);
}
function close() {
return mysql_close($this->dblink);
}
function insert_data($tname, $data) {
$values = '';
$filed = implode(',', array_keys($data));
$filednum = count($data) - 1;
//echo $filednum;
$i = 0;
foreach($data as $key => $val) {
$values .= "'".$val."'";
if($i < $filednum) {
$i++;
$values .= ',';
}
}
$sql = "INSERT INTO ".$tname." (".$filed.") VALUES(".$values.")";
return $this->unb_query($sql);
}
function update_data($tname, $fid, $data, $whereid = 'id') {
$col = array();
foreach($data as $key => $values)
{
$col[] = $key."='".$values."'";
}
$sql = "UPDATE ".$tname." SET ".implode(',', $col)." WHERE $whereid='$fid'";
return $this->unb_query($sql);
}
function error() {
return (($this->dblink) ? mysql_error($this->dblink) : mysql_error());
}
function errno() {
return intval(($this->dblink) ? mysql_errno($this->dblink) : mysql_errno());
}
function msghalt($msg = '') {
$message = "<html>n<head>n";
$message .= "<meta content="text/html; charset=utf-8" http-equiv="Content-Type">n";
$message .= "<style type="text/css">n";
$message .= "body,p,pre {n";
$message .= "font:12px Verdana;n";
$message .= "}n";
$message .= "</style>n";
$message .= "</head>n";
$message .= "<body bgcolor="#FFFFFF" text="#000000" link="#006699" vlink="#5493B4">n";
$message .= "<p>Datebase Error:</p><pre><b>".htmlspecialchars($msg)."</b></pre>n";
$message .= "<b>Mysql error number</b>: ".$this->errno()."n<br />";
$message .= "<b>Mysql error description</b>: ".htmlspecialchars($this->error())."n<br />";
$message .= "<b>Date</b>: ".date("Y-m-d @ H:i")."n<br />";
$message .= "<b>Script</b>: http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'] .'?'.$_SERVER['QUERY_STRING']."n<br />";
$message .= "</body>n</html>";
echo $message;
exit;
}
}
/**********************
$sb = new dbsql();
$sb->dbconnect('localhost','root','','fdwp');
echo 'fdfd';
****************************/
?>