PHP5面向对象查询数据的demo
时间:2006-04-18 来源:liu121027
这是本人在本地测试通过的一个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
SearchAdmin("1");
echo $obj->name;
?>
file 2:config.php
file 3:DBSql.php
DBName=$DBName;
}
$dbase=$this->DBName;
[email=$connect=@mysql_connect($this->ServerName,$this->UserName,$this->Pass]$connect=@mysql_connect($this->ServerName,$this->UserName,$this->Pass[/email]
);
if(!$connect)
{
print "Server connection error";
exit();
}
[email=$db=@mysql_select_db($dbase,$connect]$db=@mysql_select_db($dbase,$connect[/email]
);
if(!$db)
{
print ("".$dbase." Database Connection Error ");
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->positionnumber)
{
$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
";
}
function closed()
{
mysql_close($this->con);
}
}
?>
file 4:admin.php
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);
}
}
?>
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
SearchAdmin("1");
echo $obj->name;
?>
file 2:config.php
file 3:DBSql.php
DBName=$DBName;
}
$dbase=$this->DBName;
[email=$connect=@mysql_connect($this->ServerName,$this->UserName,$this->Pass]$connect=@mysql_connect($this->ServerName,$this->UserName,$this->Pass[/email]
);
if(!$connect)
{
print "Server connection error";
exit();
}
[email=$db=@mysql_select_db($dbase,$connect]$db=@mysql_select_db($dbase,$connect[/email]
);
if(!$db)
{
print ("".$dbase." Database Connection Error ");
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->positionnumber)
{
$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
";
}
function closed()
{
mysql_close($this->con);
}
}
?>
file 4:admin.php
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);
}
}
?>
相关阅读 更多 +