im trying to learn oop php while trying to create crud with oop php im getting following:
Notice: Trying to get property ‘num_rows’ of non-object in
Code are as follows:
class database{
private $host;
private $dbusername;
private $dbpassword;
private $dbname;
public $con;
protected function connect(){
$this->host = 'localhost';
$this->dbusername = 'root';
$this->dbpassword = '';
$this->dname = 'crud';
$this->con = new mysqli($this->host,$this->dbusername,$this->dbpassword,$this->dbname);
// return $con;
if($this->con->connect_error)
{
die ("<h1>Database Connection Failed</h1>");
}
return $this->con;
}
}
class wwe extends database{
public function getData(){
$sql = " SELECT * FROM user";
$result = $this->connect()->query($sql);
if($result->num_rows > 0){
$data = array();
while ($row = $result->fetch_assoc()) {
$data[] = $row;
}
return $data;
}else{
echo "No found records";
}
}
}
Table name = user