您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php面象对象数据库操作类_PHP教程

2024/5/29 22:24:26发布21次查看
host = $host; 
      $this->user = $user; 
      $this->passwd = $password; 
      $this->database = $database; 
      $this->conn=mysql_connect($this->host, $this->user,$this->passwd) or 
     die(could not connect to $this->host); 
      mysql_select_db($this->database,$this->conn) or 
     die(could not switch to database $this->database); 
   }
//该函数用来关闭数据库连接
   function close() 
   { 
      mysql_close($this->conn); 
   }
//该函数实现数据库查询操作
   function query($querystr) 
   { 
      $res =mysql_query($querystr, $this->conn) or 
      die(could not query database); 
      return $res; 
   }
//该函数返回记录集
   function getrows($res) 
   { 
      $rowno = 0; 
      $rowno = mysql_num_rows($res); 
      if($rowno>0) 
      { 
         for($row=0;$row          { 
            $rows[$row]=mysql_fetch_array($res);
            //本来为mysql_fetch_row,但是不能以数组的方式来提取,只能用索引
            //这样可以用索引和名称,更为方便
         } 
         return $rows; 
      } 
    }
//该函数取回数据库记录数
    function getrowsnum($res) 
    { 
       $rowno = 0; 
       $rowno = mysql_num_rows($res); 
       return $rowno;
    }
//该函数返回数据库表字段数
 function getfieldsnum($res)
 {
    $fieldno = 0;
    $fieldno = mysql_num_fields($res);
    return $fieldno;
 }
//该函数返回数据库表字段名称集
 function getfields($res)
 {
      $fno = $this->getfieldsnum($res);
      if($fno>0) 
      { 
         for($i=0;$i          { 
            $fs[$i]=mysql_field_name($res,$i);//取第i个字段的名称
         } 
         return $fs;
      } 
 }
}
使用时直接require该文件,然后实例化:
$sqldb = new mysqldb(localhost,root,root,testdb);
$sql = select * from tablex...;
$result = $sqldb->query($sql);//查询
$rs = $sqldb->getrows($result);//获得记录集
$num = $sqldb->getrowsnum($result);//获得记录数
...剩下的操作就是循环取值,
for($i=0;$i    echo($rs[$i][字段名]);
}
...
最后不要忘记关闭数据路连接哦$sqldb->close();当然这句可以不要,php会自动注销!但是这样能够养成一个好的习惯,最好还是加上!
...其他自己类推...不懂的可以提问!
http://www.bkjia.com/phpjc/631859.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/631859.htmltecharticle?php //******************************************************************* //此处构造一个数据库操作类,封装所有数据库操作 //可以扩展便于后台管理程序...
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product