0

config.php file

<?php
Class Config
{
    private $_host;
    private $_user;
    private $_password;
    private $_db;

    public function __constructor()
    {
        $this->_host = 'localhost';
        $this->_user = 'root';
        $this->_password = '';
        $this->_db = 'test';
    }

    public function connectDB()
    {
        $connect = new mysqli($this->_host, $this->_user, $this->_password, $this->_db);
        if (!$connect) {
            die('Connection Error : ' . $connect->mysql_error());
        } else {
            echo 'Connection Success';
        }
        return $connect;
    }
}
?>

index.php file

<?php
include 'config.php';
$conn = new Config();
$conn = $conn->connectDB();
$sql = "SELECT name, matric_no FROM users";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        echo 'Name : ' . $row['name'] . ' - Matric No : ' . $row['matric_no'] . '<br>';
    }
} ?>

Shows error:

Connection Success
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in D:\xampp\htdocs\test\index.php on line 10

Any solution?

maxhb
  • 8,255
  • 9
  • 26
  • 50

2 Answers2

2

The constructor should read as:

  • public function __construct()
  • and not public function __constructor()

As per the manual: http://php.net/manual/en/language.oop5.decon.php on Constructors and Destructors

<?php
Class Config
{
    private $_host;
    private $_user;
    private $_password;
    private $_db;

    public function __construct()
    {
        $this->_host = 'localhost';
        $this->_user = 'root';
        $this->_password = '';
        $this->_db = 'test';
    }

    public function connectDB()
    {
        $connect = new mysqli($this->_host, $this->_user, $this->_password, $this->_db);
        if (!$connect) {
           return 'Connection Error : ' . $connect->mysql_error();
        } else {
           return $connect;
        }

    }
}
?>

And in the Index.php file check whether the connection was succesfull by var_dump/print_r

<?php
include 'config.php';
$conn = new Config();
$conn = $conn->connectDB();
print_r($conn); break; //Remove this line if db connxn is succesfull

$sql = "SELECT name, matric_no FROM users";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        echo 'Name : ' . $row['name'] . ' - Matric No : ' . $row['matric_no'] . '<br>';
    }
} ?>

Keep the execution flow in your mind and debug point by point to find where it's going wrong.

Funk Forty Niner
  • 74,372
  • 15
  • 66
  • 132
Sharan Mohandas
  • 871
  • 1
  • 9
  • 25
-1

Return values of mysqli_query:

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

Most probably, you have an error in your query and mysqli_query returns FALSE.

Check mysqli_error($conn) for the error message.

Mihai Răducanu
  • 10,872
  • 4
  • 21
  • 29