初入php,有什么说错的请指出来啊。谢谢了。目前情况是这样的,
有一个db_config.php文件,里面是这样的:class DbConfig{
private $db_url = 'localhost:3333';
private $db_user = 'root';
private $db_pwd = 'root';
private $db_name = 'xinxiwang';
function __get($property_name){
return $this->$property_name;
}

public function getConn(){
$link = mysql_connect($this->db_url,$this->db_user,$this->db_pwd) or die("can not connect to Mysql server");
mysql_select_db($this->db_name,$link) or die("can not found database " . $this->db_name );
return $link;
}
}然后有一个对象类,里面有一些操作,比如里面有数据查询的方法
/**
* function: 从数据库中查询指定页数的友情链接
* parameter: 数据库连接,页码,每页显示数
* 返回指定页数数据
*/function fri_select($db,$pagenum,$counts){
$sql = "call friendlink_select($pagenum,$counts)";
$result = mysql_query($sql,$db);
mysql_close($db);
return $result;

}
以及统计用的方法/**
* function:统计友情链接数
* parameter:数据库连接
* 返回链接总数
*/function fri_count($db){
$sql = "call friendlink_counts()";
$result = mysql_query($sql,$db);
if($row = mysql_fetch_array($result)){
$num = $row[0];
}
mysql_free_result($result);
mysql_close($db);
return $num;
}现在第三个展示页面里会调用这两个方法,
$db = new DbConfig();
$dbs = $db->getConn();
$pages = fri_count($dbs);
$result = fri_select($dbs,1,6);目前我的问题是:1.外面通过$dbs = $db->getConn();得到的链接对象,传到方法里,可以在方法里close它吗?
2.在第一个方法里,它被close掉了,那传到第二个里面,应该为空了吧。第二个应该会出错吧,那么,如何解决这个问题呢?就是让连接对象在第二个方法也可以用,难道再来一个$dbs2 = $db->getConn();,将$dbs2传到第二个方法里吗?
3.如果有更多的方法要被调用,是不是要$dbs3,$dbs4....$dbsn???是这样吗?有更简洁的方法吗?谢谢各位了。

解决方案 »

  1.   

    还有个问题,就是在fri_select方法中,我没有释放$result这个结果,而是把它return了,那么,当跳出这个方法后,这个会被自动释放吗?还是有别的方法释放 、???刚接解php的对象,不知道写的对不对。
      

  2.   

    啊,我刚刚在第三个页面试了下,
    if($dbs){
    echo "第一个连接存在,<br>";
    }else {
    echo "sorry,the first is close <br>";
    }
    居然会输出“第一个连接存在”,难道是没有关闭吗、?
    我把它放在方法里面,在close后面,它也是显示的这句话,为什么啊????
      

  3.   

    好吧,大家看不起我的90分,最后去google的国外论坛发贴求助解决了下!!!哎,为什么我们聊天的都那么火呢。
      

  4.   

    你觉得你在方法内  mysql_close() 能关掉外面的 $DB 么。 如果你用 引用传值的 是可以的。function fri_count(&$db){
      

  5.   

    国外这个时候是白天~,你13点在CSDN发帖然后再在国外论坛发帖,你看看谁快。
      

  6.   


    有个老外是这么说的:
    $dbs is a resource, not a true/false value as to whether the connection 
    is alive or not.  Even after closing the connection, the resource is 
    still there - but not the connection. 然后我觉得这个已经关闭了。因为把它传给别一个连接里,会说resource为空。