Heim  >  Artikel  >  php教程  >  php 如何导入.sql文件

php 如何导入.sql文件

WBOY
WBOYOriginal
2016-06-08 17:30:121671Durchsuche
<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';
****************************/
?>

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:php入门:保存数据到数据库Nächster Artikel:php生成 execel表格