Home  >  Article  >  Backend Development  >  关于PHP实例化对像, 急

关于PHP实例化对像, 急

WBOY
WBOYOriginal
2016-06-13 12:11:27733browse

关于PHP实例化对像,在线等 急急急

本帖最后由 youxunet 于 2014-12-10 12:57:00 编辑 为什么我这么做。不行呢?
$db=new db;
class db
{ function sql($sql){
global $conn;  //数据库
$query = $conn->query($sql);
$rs = mysql_fetch_array($query);
return $rs;
mysql_free_result($query);

}


function Listname(){
$id =Cut(intval($weburl[2]));
$rs = $db->sql("select title from `column` where id = ".$id); 
return $rs[0];
}

报错:
Notice: Undefined variable: db in 
Fatal error: Call to a member function sql() on a non-object in 

14行出错 第14行:$rs = $db->sql("select title from `column` where id = ".$id); 
------解决思路----------------------
$query = $conn->query($sql);
改成
$query =mysql_query($sql,$conn);
------解决思路----------------------
$db=new db;
这行放到 Listname函数里面去,或者说在Listname函数中调用到$db之前用global把$db全局化。
你既然在sql函数中使用了global关键字全局化了$conn变量,说明你是知道作用域的,怎么还会犯这样的错误
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn