首頁 > php教程 > PHP源码 > 主體

php 如何导入.sql文件

WBOY
發布: 2016-06-08 17:30:12
原創
1654 人瀏覽過
<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';
****************************/
?>

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!