-1

Hello this sound easy but something seems to be wrong here,

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();

}

}

on implimenting the above class

 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();
    }

on running i recieve the error: Fatal error: Call to a member function fetch_array() on a non-object in C:\wamp\www\PHP5_Testing\classes\class.execute.php on line 20

can anyone help me to fix this problem? when i used procedural mysqli it worked fine but the problem is now with oop mysqli

1 Answers1

0

when you use OOP , you must follow the rules!

some problem that i see in your codes , here :

change

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

}

into :

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

}

and

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

into

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

hope this fix your problem.

  • 1
    inbefore the author is going to say it doesnt work, thats because he's using an undefined var `$row` in his `try{}catch{}`! –  Mar 07 '16 at 09:27
  • 1
    @Neat , you right , but , all of his code have problem! . he don't follow the rules. when he try to `$execute->Fetch()` (before $row...) he face the error –  Mar 07 '16 at 09:30
  • Can confirm that it is a mess :) –  Mar 07 '16 at 09:31
  • Perfect i think you have shade some light, dont worry about the code, i was merely used to procedural programming – Nyakwar Nyamittah Majunior Mar 07 '16 at 09:38