1

I have this code

class Db_conn {
    private $sn = 'localhost';
    private $un = 'root';
    private $up = '';
    public function connect(string $db_n){
        $conn = mysqli_connect($this->sn, $this->un, $this->up, $db_n);
        if (!$conn) {
            die("Připojení se nezdařilo: " . mysqli_error($conn));
        } else {
            return $conn;
        }
    }
}

And this code

public function update($query){
    $dbconn = new Db_conn();
    if (mysqli_query($dbconn, $query)) {
        return True;
    } else {
        return False;
    }
}

And on this line if (mysqli_query($dbconn, $query)) { it says there is an error.

Warning: mysqli_query() expects parameter 1 to be mysqli, object given in D:\xampp\htdocs\purkiada2\content\Db_parser.inc.php on line 21

Dharman
  • 30,962
  • 25
  • 85
  • 135
Filip Bartoš
  • 301
  • 3
  • 16
  • This: `if (mysqli_query($dbconn, $query))`. You're literally creating an instance of `Db_conn` and passing that in when you should probably be using your connection method `if (mysqli_query($dbconn->connect('your_database_name'), $query))`. – cteski Jan 23 '17 at 21:18
  • where you select your DB, How you pointed your DB – BetaDev Jan 23 '17 at 21:19
  • Thanks I used that solution – Filip Bartoš Jan 23 '17 at 21:21

1 Answers1

4

mysqli_query expects a mysqli connection, but you give it a Db_conn object. This is what the error message says.

You must first connect and then give this (mysqli) connection to mysqli_query, e.g.

public function update($query){
    $dbconn = new Db_conn();
    $conn = $dbconn->connect();
    if (mysqli_query($conn, $query)) {
        return True;
    } else {
        return False;
    }
}
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198