我写了个类,但是在function 里用不了

jeiryc 2009-12-31 03:41:55
我写了个类,但是在function 里用不了 请高手赐教。

这里是 db.php
class DB{
private $dbhost="dbhost"; //default host and port
private $dbuser="dbuser";//default mysql user
private $dbpass="dbpass";//default mysql passwd
private $dbname="dbname";//default database

function __construct($hostport,$username,$password,$database){
$this->dbhost=$hostport;
$this->dbuser=$username;
$this->dbpass=$password;
$this->dbname=$database;
}

public function selectSQL($sql,$option){
$link = mysql_connect($this->dbhost,$this->dbuser,$this->dbpass) or exit("Cannot connect to the db server!");
$tof1 = mysql_select_db($this->dbname) or exit("Cannot select the database!");
$sql0 = "set names utf8";
$sql1 = $sql;
$res0 = mysql_query($sql0) or exit ("Cannot execute the query sql0!");
$res1 = mysql_query($sql1) or exit ("Cannot execute the query sql1!");

switch($option){
case 'noReturn': return $res1; break;//execute non-query operation
case 'defaultQuery':{
$result=array();
while($record=mysql_fetch_row($res1)){
$result[]=$record;
}
$tof2=mysql_free_result($res1) or exit("Cannot free the result!");
return $result;
}break;
//other case added here
default: break;
}
}

public function insert($sql){ return $this->selectSQL($sql,'noReturn'); }

public function delete($sql){ return $this->selectSQL($sql,'noReturn'); }

public function update($sql){ return $this->selectSQL($sql,'noReturn'); }

public function select($sql){ return $this->selectSQL($sql,'defaultQuery'); }

}

$db=new DB(dbhost,dbuser,dbpass,dbname);


这是我的类。我这样引用



function load_class($conditions1){
//include_once 'conn.php';
include_once('feipicadmin/db.php');//类文件
$result = $db->select("select channel_folder from feipic_chennel where channel_id = $conditions1");
for($i=0;$i<count($result);$i++){
echo = $result[$i][0];
}
//
}
load_class($conditions1=2);


输出结果是

Fatal error: Call to a member function select() on a non-object in H:\APMServ5.2.6\www\htdocs\feipic\head.php on line 15


是不是我要加上全局变量???我要怎么改才可以?要怎么加,可以加一下给我看吗?
...全文
70 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lenovor61i 2009-12-31
  • 打赏
  • 举报
回复
上面那个调用的时候少传了一个参数哈,代码说话demo


<?php
class test{
function print_echo(){
echo 'right';
}
}
$db = new test();
function test2(&$db) //把对象传进去
{
$db->print_echo();
}
test2(&$db); //调用
?>

江南昆虫 2009-12-31
  • 打赏
  • 举报
回复
load_class函数内要global $db;或者$GLOBALS['db']->select
lenovor61i 2009-12-31
  • 打赏
  • 举报
回复
你把,$db=new DB(dbhost,dbuser,dbpass,dbname);
产生的$db 传参传进去吧,

function load_class($conditions1,&$db){
//include_once 'conn.php';
include_once('feipicadmin/db.php');//类文件
$result = $db->select("select channel_folder from feipic_chennel where channel_id = $conditions1");
for($i=0;$i<count($result);$i++){
echo = $result[$i][0];
}
//
}
load_class($conditions1=2);

jaxio 2009-12-31
  • 打赏
  • 举报
回复
select() 你写成函数,我真汗。。。。
phpboy 2009-12-31
  • 打赏
  • 举报
回复
把 include_once 改成 include 试试
kxgsy163 2009-12-31
  • 打赏
  • 举报
回复
global $db 下。在head.php里

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧