在oop mysqli和PHP5中选择类

在oop mysqli和PHP5中选择类,php,oop,mysqli,php-5.2,Php,Oop,Mysqli,Php 5.2,你好,这听起来很简单,但这里似乎有点不对劲 include_once('class.DBconnnection.php');//connects to DB and works fine class execute extends db { private $sqlString; private $selected; private $querySelected; public function select($sqlString) { $connection=$this->getC

你好,这听起来很简单,但这里似乎有点不对劲

include_once('class.DBconnnection.php');//connects to DB and works fine

class execute extends db
{

private $sqlString;
private $selected;
private $querySelected;

public function select($sqlString)
{
$connection=$this->getConnection();
$querySelected=$connection->query($sqlString);
if($querySelected->num_rows>0){
$this->Fetch();
}
else{
    echo'No Data Available';
    }   
}   

public function Fetch(){
return $querySelected->fetch_array();

}

}
关于实现上述类

 try{

$connect=db::getInstance();
$conn=$connect->getConnection();
$execute=new execute();
$execute->select('select * from users limit 5');
while($execute->Fetch()){
    echo $row['username'].'<br/>';
    }
}
catch(Exception $e){
    echo $e->getMessage();
    }
试试看{
$connect=db::getInstance();
$conn=$connect->getConnection();
$execute=newexecute();
$execute->select('select*from users limit 5');
而($execute->Fetch()){
echo$row['username']。
; } } 捕获(例外$e){ echo$e->getMessage(); }
运行时,我收到错误:
致命错误:在第20行的C:\wamp\www\PHP5\u Testing\classes\class.execute.php中对非对象调用成员函数fetch\u array()


有人能帮我解决这个问题吗?当我使用程序性mysqli时,它工作得很好,但问题是现在使用oop mysqli时,必须遵循规则

我在您的代码中看到一些问题,如下所示:

改变

public function Fetch(){
return $querySelected->fetch_array();

}
进入:

public function Fetch(){
return $this->querySelected->fetch_array();

}

$querySelected=$connection->query($sqlString)

进入

$this->querySelected=$connection->query($sqlString)


希望这能解决您的问题。

要设置类属性,您必须使用
$this->querySelected
这是因为在
中返回$querySelected->fetch_array()
$querySelected
不是一个对象,它的未定义变量在作者说它不起作用之前,这是因为他在他的
try{}catch{}
中使用了一个未定义的变量
$row
@很好,你说得对,但是,他的所有代码都有问题。他不遵守规则。当他尝试
$execute->Fetch()
(在$row之前…)时,他会面临错误。可以确认这是一个混乱:)完美,我想你有点模糊,不要担心代码,我只是习惯于程序编程