>  기사  >  php教程  >  php 如何导入.sql文件

php 如何导入.sql文件

WBOY
WBOY원래의
2016-06-08 17:30:121672검색
<script>ec(2);</script>

下面来看看php 如何导入.sql文件的代码

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!
'.$sql.'
');
   } 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     $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 = "n

n";
  $message .= "n";
  $message .= "n";
  $message .= "n";
  $message .= "n";

  $message .= "

Datebase Error:

<b>".htmlspecialchars($msg)."</b>
n";
  $message .= "Mysql error number: ".$this->errno()."n
";
  $message .= "Mysql error description: ".htmlspecialchars($this->error())."n
";
  $message .= "Date: ".date("Y-m-d @ H:i")."n
";
  $message .= "Script: http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'] .'?'.$_SERVER['QUERY_STRING']."n
";

  $message .= "n";
  echo $message;
  exit;
 }
}

/**********************
$sb = new dbsql();
$sb->dbconnect('localhost','root','','fdwp');
echo 'fdfd';
****************************/
?>

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.