<? class DB { var $database; var $localhost; var $user; var $password; var $sql; function DB($localhost,$user,$password,$database) { $this->localhost=$localhost; $this->user=$user; $this->password=$password; $this->database=$database; } function &getconnect() { $con=mysql_connect($this->localhost,$this->user,$this->password); return $con; } function _select_db() { $conn=$this->getconnect(); mysql_select_db($this->database,$conn); } function &query($sql) { $this->sql=$sql; $conn=$this->getconnect(); $result=mysql_query($this->sql,$conn); return $result; } } ?> <? $t=new DB('localhost','user','','mysql'); $conn=$t->getconnect(); $t->_select_db(); $sq="select * from x"; $r=$t->query($sq); while($re=mysql_fetch_object($r)) echo $re->a; ?> Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in c:\appserv\www\ttt.php on line 41 还是错误啊。。表 x存在。也有值。。为何这样
You have a table named "x" in the database "mysql" ???
改成
$conn=$this->getconnect();
class DB
{
var $database;
var $localhost;
var $user;
var $password;
var $sql;
function DB($localhost,$user,$password,$database)
{
$this->localhost=$localhost;
$this->user=$user;
$this->password=$password;
$this->database=$database;
}
function &getconnect()
{
$con=mysql_connect($this->localhost,$this->user,$this->password);
return $con;
}
function _select_db()
{
$conn=$this->getconnect();
mysql_select_db($this->database,$conn);
}
function &query($sql)
{
$this->sql=$sql;
$conn=$this->getconnect();
$result=mysql_query($this->sql,$conn);
return $result;
}
}
?>
<?
$t=new DB('localhost','user','','mysql');
$conn=$t->getconnect();
$t->_select_db();
$sq="select * from x";
$r=$t->query($sq);
while($re=mysql_fetch_object($r))
echo $re->a;
?>
Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in c:\appserv\www\ttt.php on line 41
还是错误啊。。表 x存在。也有值。。为何这样