首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 网站建设 > PHP > PHP5面向对象查询数据的demo
【标  题】:PHP5面向对象查询数据的demo
【关键字】:PHP5,demo
【来  源】:http://www.cublog.cn/u/17267/showart.php?id=101480

PHP5面向对象查询数据的demo

这是本人在本地测试通过的一个demo,把数据库的表看作一个对象(这里只能得到一个记录的对象)进行各种操作。
 
Sql:
#----------------------------
# Table structure for users
#----------------------------
CREATE TABLE `users` (
  `id` int(50) unsigned NOT NULL auto_increment,
  `name` varchar(50) NOT NULL default '',
  `type` varchar(50) NOT NULL default '',
  `login` varchar(12) NOT NULL default '',
  `password` varchar(50) NOT NULL default '',
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
#----------------------------
# Records for table users
#----------------------------
 
insert  into users values
(1, 'admin', 'admin', 'yes', '123456'),
(2, 'Hok', 'admin', 'admin', 'admin');
 
//////////////////////////////////////////////////////////////////////////
file 1:index.php
 
<?
include "DBSql.php";
include "admin.php";
$obj=new Admin();
$obj->SearchAdmin("1");
echo $obj->name;
?>
 
 
file 2:config.php
<?
define ("UserName","root");
define ("PassWord","liu");
define ("ServerName","localhost");
define ("DBName","admin");
?>

file 3:DBSql.php

<?
include "config.php";
class DBSql{
     private $con="";
  private $UserName=UserName;
  private $Pass=PassWord;
  private $DBName=DBName;
  private $ServerName=ServerName;
  public $Dao;
  private $number;
  private $Result;
 

  function __construct($DBName='')
 {

   if(!empty($DBName))
   {
           $this->DBName=$DBName;
   }

   $dbase=$this->DBName;

   $connect=@mysql_connect($this->ServerName,$this->UserName,$this->Pass);

   if(!$connect)
  {
    print "<p>Server connection error</p>";
    exit();
  }

  $db=@mysql_select_db($dbase,$connect);

  if(!$db)
  {
   print ("<p>".$dbase." Database Connection Error </p>");
   exit ();
  }

  $this->con=$connect;
  return true;
  }

  function Query($sql)
 {
      if(empty($sql)) return false;
   if(empty($this->con)) return false;
   $results=mysql_query($sql,$this->con);
   if((!$results) or empty($results))
  {
    mysql_free_result($results);
    print "Error".mysql_error();
    return false;
  }

   return $results;
   
 }

 function Num($sql)
 {
  return mysql_num_rows($this->Query($sql));
 }

 function Fetch_row($sql)
 {
  $results=$this->Query($sql);
  $data=mysql_fetch_array($results);
  return $data;
 }

 function Fetch_array($query)
 {
  $data = @mysql_fetch_array($query);
  return $data;
 }

 function Insert_Id($sql)
 {
  $results = $this -> Query ($sql);
  $id = @mysql_insert_id ();
  return $id;
 }
 
 function executeSql($sql)
 {
  $this->Result=$this->Query($sql);
  $this->position=0;
  $this->number=$this->Num($sql);
   if($this->number>0)
  {
   $this->abDao();
   $this->DaoVariables();
  }
  return true;
 }

  
 function abDao()
 {
  $this->Dao=null;
  if($this->position<$this->number)
  {
   $field=$this->Fetch_array($this->Result);
   $this->Dao[$this->position]=$field;
  
  }
 }

   function Select($sql)
 {
    $results=$this->Query($sql);
    $count=0;
    $data=array();
    while($row=mysql_fetch_array($this->results))
  {
     $data[$count]=$row;
     $count++;
  }
  mysql_free_result($results);
  return $data;
 }

  function DaoVariables()
 {
 }


 function __set($name,$value)
 {
  $this->$name=$value;
 }

 function __get($name)
 {
  print "The method does not exist <br>";
 }
 
 function closed()
    {

     mysql_close($this->con);

 }
 }
 

?>

file 4:admin.php

<?
class Admin extends DBSql{
 public $id;
 public $name;
 public $type;
 public $login;
 public $password;

    function __construct()
 {
  parent::__construct();
 }

 function DaoVariables()
 {
  $this->id=$this->Dao[$this->position]["id"];
  $this->name=$this->Dao[$this->position]["name"];
  $this->type=$this->Dao[$this->position]["type"];
  $this->login=$this->Dao[$this->position]["login"];
  $this->password=$this->Dao[$this->position]["password"];

    }

 function SearchAdmin($id='',$name='',$type='')
 {
  $sql="select * from users";
  if(!empty($id))
  {
   $sql.=" where id=".$id;
  }
  $this->executeSql($sql);
 
  
  
 }
}

?>

 


 
 
 
 
 
隐藏下载地址:【上一篇】
手机电子书下载:【下一篇】
【相关文章】
  • 编译安装apache2+php5+mysql4+ZendOptimizer+Cronolog简易文档
  • 配置Apache2+PHP5+MYSQL5
  • php5学习笔记
  • WINDOWS下php5环境的安装与配置(1)
  • WINDOWS下php5环境的安装与配置(2)
  • 为面向对象而生的PHP5
  • 《实战 Web Service 压缩传输》的PPT和Demo
  • http2+PHP5 + MySQL5 + GD2 + Proftpd + php
  • Apache 2.0.50,+php5.1.2+mysql 5.1 安装手记
  • RedHat_AS4下安装Sybase12.5企业版+php5.1.1+apache2.0.55
  • 【随机文章】
  • Web服务器大扫除_三、必须运行的进程
  • WM5 中起用OBEX实现蓝牙收发文件
  • G.652与G.655光纤在传输网上的应用
  • suse整理资料
  • SUN工程师给我的回信!
  • 继续加入FMOD,替代DSOUND来处理音效和音乐。
  • 天堂2 驳网络老婆二十则
  • 感受JAVA跨平台
  • 优化数码照片的简单处理方法
  • C语言一
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.