相思资源网 Design By www.200059.com

本文实例讲述了PHP实现的mysql操作类。分享给大家供大家参考,具体如下:

首先是mysql方式

<"localhost";
  //数据库的username
  private $name="root";
  //数据库的password
  private $pass="";
  //数据库名称
  private $table="phptest";
  //编码形式
  private $ut="utf-8";
  //构造函数
  function __construct(){
    $this->ut=$ut;
    $this->connect();
  }
  //数据库的链接
  function connect(){
    $link=mysql_connect($this->host,$this->name,$this->pass) or die ($this->error());
    mysql_select_db($this->table,$link) or die("没该数据库:".$this->table);
    mysql_query("SET NAMES '$this->ut'");
  }
  function query($sql, $type = '') {
    if(!($query = mysql_query($sql))) $this->show('Say:', $sql);
    return $query;
  }
  function show($message = '', $sql = '') {
    if(!$sql) echo $message;
    else echo $message.'<br>'.$sql;
  }
  function affected_rows() {
    return mysql_affected_rows();
  }
  function result($query, $row) {
    return mysql_result($query, $row);
  }
  function num_rows($query) {
    return @mysql_num_rows($query);
  }
  function num_fields($query) {
    return mysql_num_fields($query);
  }
  function free_result($query) {
    return mysql_free_result($query);
  }
  function insert_id() {
    return mysql_insert_id();
  }
  function fetch_row($query) {
    return mysql_fetch_row($query);
  }
  function version() {
    return mysql_get_server_info();
  }
  function close() {
    return mysql_close();
  }
  //向$table表中插入值
  function fn_insert($table,$name,$value){
    $this->query("insert into $table ($name) value ($value)");
  }
  //根据$id值删除表$table中的一条记录
  function fn_delete($table,$id,$value){
    $this->query("delete from $table where $id=$value");
    echo "id为". $id." 的记录被成功删除!";
  }
}
$db = new ConnectionMySQL();
$db->fn_insert('test','id,name,sex',"'','hongtenzone','M'");
$db->fn_delete('test', 'id', 1);
"font-size: medium">mysqli的方式:

<"\n<div class=\"pagenav\">";
   $PageHtml .= '<span>' . $this->PageCount . '条记录    ' . $this->PageNo . '/' . $this->PageNum . '页</span>      ';
   $this->PageNo <= 1 or $PageHtml .= "<a href=\"" . $NaIndex . "\">首页</a>\n<a href=\"" . $NavUrl . ($this->PageNo - 1) . "\">上一页</a>\n";
  if ( $NumNav ) { $PageHtml .= $this->NumPage($NavUrl); }
  $this->PageNo >= $this->PageNum or $PageHtml .= "<a href=\"" . $NavUrl. ($this->PageNo + 1) . "\">下一页</a>\n<a href=\"" . $NavUrl . $this->PageNum . "\">尾页</a>\n";
  $PageHtml .= "</div>\n";
  return $PageHtml;
  }
  private function NumPage( $Can = '' ) { //数字分页
    $NumHtml = '';
    $First  = 1;
    $Last  = $this->PageNum;
    if ( $this->PageNum > 5 ) {
      if ( $this->PageNo < $this->PageNum ) {
       $First = $this->PageNo - 2;
       $Last = $this->PageNo + 2;
      }else{
       $First = $this->PageNo - 4;
       $Last = $this->PageNum;
      }
    }
    if ( $First < 1 ) { $First  = 1; $Last = $First + 4;}
    if ( $Last > $this->PageNum ) { $First  = $this->PageNum - 4; $Last = $this->PageNum;}
    for( $i = $First; $i <= $Last; $i++) {
      $NumHtml .= $this->PageNo != $i "\n\t" . '<a href="' . $Can . $i . '" rel="external nofollow" >' . $i . '</a>' . "\n\t" : "\n\t" .'<a class="hover" disabled="disabled">' . $i . '</a>' . "\n\t";
    }
    unset($Can, $First, $i, $Last);
    return $NumHtml;
  }
  public function UserPage($NumNav = false, $PageName = 'index', $Mulu = 'user' ) { //会员中心分页
   $NavUrl  = '/' . $Mulu . '/' . $PageName . '/' . $this->PageKey . '/';
   $PageHtml = "\n<div class=\"pagenav\">";
   $PageHtml .= '<span>' . $this->PageCount . '条记录    ' . $this->PageNo . '/' . $this->PageNum . '页</span>      ';
   $this->PageNo <= 1 or $PageHtml .= "<a href=\"" . $NavUrl . "1\">首页</a>\n<a href=\"" . $NavUrl . ($this->PageNo - 1) . "\">上一页</a>\n";
  if ( $NumNav ) { $PageHtml .= $this->NumPage($NavUrl); }
  $this->PageNo >= $this->PageNum or $PageHtml .= "<a href=\"" . $NavUrl. ($this->PageNo + 1) . "\">下一页</a>\n<a href=\"" . $NavUrl . $this->PageNum . "\">尾页</a>\n";
  $PageHtml .= "</div><div class=\"clear\"></div>\n";
  return $PageHtml;
  }
  //表单处理开始
  //判断表单时候提交
  public function FormIs( $Keys = 'mm' ) {
    return $_POST[ $Keys ] == 1 "Content-Type" content="text/html; charset=utf-8" />';
    echo '<script type="text/javascript">
 <!--
  alert("' . $Text . '");
  document.location="' . $_SERVER['HTTP_REFERER'] . '";
 //-->
</script>';
 exit;
  }
  #取得系统当前时间
  public function Times(){
    return str_replace('-', '[jian]', date('Y-m-d H:i:s'));
  }
  #取得用户IP地址
  public function GetIP(){
   if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
    $ip = getenv("HTTP_CLIENT_IP");
    else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
    $ip = getenv("HTTP_X_FORWARDED_FOR");
    else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
    $ip = getenv("REMOTE_ADDR");
    else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
    $ip = $_SERVER['REMOTE_ADDR'];
    else
    $ip = "unknown";
   return($ip);
  }
  //最后关闭数据库连接
  public function Close(){
    !is_object( $this -> Conn ) or mysqli_close( $this -> Conn );
  }
}

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php+mysqli数据库程序设计技巧总结》、《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

标签:
PHP,mysql,操作类

相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com

评论“PHP实现的mysql操作类【MySQL与MySQLi方式】”

暂无PHP实现的mysql操作类【MySQL与MySQLi方式】的评论...