天空是蓝色的

做好软件为中国 #gcc -c helloworld.c -o helloworld.o //编译目标文件 #gcc helloworld.o -o helloworld //编译成可执行exe #helloworld //运行exe
数据加载中……
PHP写了个连接Mysql数据库的类
class.mysqlconnector.php
<?php
class MysqlConnector{
var $dbname;//数据库名称
var $dbhost;//数据库地址
var $dbusername;//数据库用户名
var $dbpassword;//数据库密码



    
function MysqlConnector(){
        
$this->dbname = "test1";  //还是想写个set()方法来封装"私有"变量
        $this->dbhost = "localhost";
        
$this->dbusername = "root";
        
$this->dbpassword = "";
    }

    
function connectMySql(){
        
$openconn = mysql_pconnect($this->dbhost,$this->dbusername,$this->dbpassword ) or die("can not connect to mysql database!");
        
mysql_select_db($this->dbname,$openconn);
        
return $openconn;
    }

}
?>

test.php
<?php
require("class.mysql.connector.php");
$mysql_connector = new MysqlConnector();
$conn = $mysql_connector->connectMySql();

$rs = mysql_query("select * from student",$conn);
while($row = mysql_fetch_object($rs)){
    
print ($row->id .".".$row->name.".".$row->age."<br/>");
}

?>

student.sql
create table student (
     id 
int(10) unsigned not null default '0',
     name 
varchar(10),
     age 
varchar(10)
     );
insert into student values (1,'xiaoli','23');
insert into student values (2,'xiaoshi','23');   
insert into student values (3,'xiaobai','24');        

测试环境
http://jaist.dl.sourceforge.net/sourceforge/appserv/appserv-win32-2.5.5.exe

http://www.appservnetwork.com/

Apache 2.0.55
MySQL 5.0.15
PHP 5.1.1
phpMyAdmin-2.6.4-pl4


运行效果
{2117F59D-8885-4110-AE41-85CB4E04D9FE}0.jpg

posted on 2005-12-28 15:34 bluesky 阅读(2272) 评论(6)  编辑  收藏 所属分类: PHP

评论

# re: PHP写了个连接Mysql数据库的类 2006-11-22 15:43 特色通

言简意赅,不错,很是和初学者
  回复  更多评论    

# re: PHP写了个连接Mysql数据库的类 2007-04-26 16:16 windlike

我测试了,不错,谢谢。
  回复  更多评论    

# re: PHP写了个连接Mysql数据库的类 2007-04-26 16:19 windlike

忘了说,有个变量写错了,应该是$this->dbpassword, 而不是$this->dbuserpwd
  回复  更多评论    

# re: PHP写了个连接Mysql数据库的类 2007-07-26 08:55 learsu

这样写类吗?简单封装一下函数就叫类呀?你连资源都不释放?
  回复  更多评论    

# re: PHP写了个连接Mysql数据库的类 2011-09-07 15:32 r

  回复  更多评论    

# re: PHP写了个连接Mysql数据库的类 2011-12-30 09:52 Horny Goat Weed

這裡尼斯帖子老兄,真的很期待,以檢查出新的東西太多的人,它是所有真正的頂級信息,使該歡呼:)
  回复  更多评论    

只有注册用户登录后才能发表评论。


网站导航: