-3

My code is not working. Error is:

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\xampp12\htdocs\Website\dbconnect2.php on line 26

This my code:

<?php class DBController {
private $host = "localhost";
private $user = "Admin";
private $password = "1234";
private $database = "dbtest";

function __construct() {
    $conn = $this->connectDB();
    if(!empty($conn)) {
        $this->selectDB($conn);
    }
}

function connectDB() {
    $conn = mysqli_connect($this->host,$this->user,$this->password);
    return $conn;
}

function selectDB($conn) {
    mysqli_select_db($conn, $this->database);
}

function runQuery($query) {
    $result = mysqli_query($this->connectDB(),$query);
    while($row=mysqli_fetch_assoc($result)) {
        $resultset[] = $row;
    }       
    if(!empty($resultset))
        return $resultset;
}

function numRows($query) {
    $result  = mysqli_query($query);
    $rowcount = mysqli_num_rows($result);
    return $rowcount;       } } ?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Read the documentation and you know what is wrong: http://php.net/manual/de/mysqli.query.php – Jens Apr 20 '17 at 10:27

1 Answers1

0

In the runQuery function, you are using mysqli_query in wrong way. Correct way is

 mysqli_query($this->connectDB(),$query);

where $conn is the mysql object which is build with mysqli_connect function.

Davinder Kumar
  • 652
  • 4
  • 17
  • **It show this error now.** ** Notice: Undefined variable:** conn in C:\xampp12\htdocs\Website\dbconnect2.php on line 25 **Warning:** mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp12\htdocs\Website\dbconnect2.php on line 25 **(Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in C:\xampp12\htdocs\Website\dbconnect2.php on line 26)** – Mubasher Salam Apr 20 '17 at 10:50
  • @MubasherSalam Don't just copy paste the code. Look for the connection variable build with mysqli_connect and replace that variable with $conn – Davinder Kumar Apr 20 '17 at 10:53
  • I use your correct way but it shows up one errors. I fix all errors just that 2 left that why I posted. can you fix that and send me again. – Mubasher Salam Apr 20 '17 at 11:00
  • @MubasherSalam I have edited the answer. – Davinder Kumar Apr 20 '17 at 11:04
  • Thanks it works now but only one more error left can you help me on that. can you check below mysqli_query because that have some boolean error – Mubasher Salam Apr 20 '17 at 11:08
  • Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\xampp12\htdocs\Website\dbconnect2.php on line 26 – Mubasher Salam Apr 20 '17 at 11:08
  • Mark it accepted if it works for you Your query is wrong. Show error with this, $result = mysqli_query($query) or die(mysqli_error($this->connectDB())); – Davinder Kumar Apr 20 '17 at 11:11
  • You can mark accepted answer like this. http://i.prntscr.com/724f532f73574282b5335bda587e429f.png just click that to accept so other users can take advantage from it. – Davinder Kumar Apr 20 '17 at 11:12
  • still have problem – Mubasher Salam Apr 20 '17 at 11:16
  • Yes obviously, the code i shared will let you know the exact error. Follow that error and correct the query. – Davinder Kumar Apr 20 '17 at 11:17
  • still showing that boolean error – Mubasher Salam Apr 20 '17 at 11:19
  • Replace this code $result = mysqli_query($query); with $result = mysqli_query($query) or die(mysqli_error($this->connectDB())); everywhere. – Davinder Kumar Apr 20 '17 at 11:20
  • now this showing mysqli_query() expects at least 2 parameters, 1 given in – Mubasher Salam Apr 20 '17 at 11:25