-1

I am trying to write a wrapper class for mysqli, basically just to better handle errors, but I'm getting an error when I try to run any page on the website, not just specifically ones that call mysqli.

this is the error I get:

PHP Warning: Declaration of PricingMySql::query($query) should be compatible with mysqli::query($query, $resultmode = NULL) in \classes\PricingMySql.php on line 37

This is the class definition for PricingMySql:

class PricingMySql extends mysqli{

public function __construct() {
    parent::__construct(Pricing_DBServerName, Pricing_UserName,
            Pricing_Password, Pricing_DBName);
    if($this->connect_errno){
        throw new Exception("Failed to connect to MySQL: (". $this->connect_errno. ") ". $this->connect_error, $this->connect_errno);
    }
}

public function query(string $query, int $resultmode = MYSQLI_STORE_RESULT) {
    if(!$result  = parent::query($query, $resultmode)) {
        echo "Query Execution failed: (", $this->connect_errno, ") ", $query;
    }
    return $result;
}

According to php.net, the definition of the mysqli::query method is exactly the same

 mysqli::query ( string $query [, int $resultmode = MYSQLI_STORE_RESULT ] ) : mixed

So why then do I get the warning? I tried changing my code so that $resultmode = null like it says in the warning:

public function query(string $query, int $resultmode = NULL) {

but that still results in the same warning. Where am I going wrong?

Edit to add: I got rid of the forced types:

public function query($query, $resultmode = MYSQLI_STORE_RESULT) {

but again, same warning.

Mike vG.
  • 31
  • 1
  • 7
  • The clue is in the error... Mysql vs. Mysqli – Adam Feb 12 '19 at 16:23
  • `MySql::query($query)` should be compatible with `mysqli::query` – Adam Feb 12 '19 at 16:23
  • Somewhere you are using a mysql() function and then mysqli() function... or similar.. but can't see it in your code... Post more code. – Adam Feb 12 '19 at 16:24
  • You have extended the `MYSQLI` Class and then you have tried to redefine the prototype of `MYSQLI::query()` It wont let you do that. Look at the prototype of [mysqli::query](http://php.net/manual/en/mysqli.query.php) – RiggsFolly Feb 12 '19 at 16:25
  • My bad.. read it wrong... PricingMySql::query($query, null) <--- you've tried that? – Adam Feb 12 '19 at 16:26
  • Get rid of the forced types (`string` and `int`) in the signature. Sometimes less is more. – mario Feb 12 '19 at 16:26
  • I did get rid of the forced types, it still gives the same warning. – Mike vG. Feb 12 '19 at 16:30
  • Can you confirm if you have tried this PricingMySql::query($query, null) not in the method declaration but the method call? – Adam Feb 12 '19 at 16:32
  • @Dammeul it doesn't even get that far. This warning occurs when the PricingMySql.php class is loaded via require_once(); Besides, what would be the point of having a default on the parameter if it's going to cause an error when you don't use it in the call? – Mike vG. Feb 12 '19 at 16:42
  • OK, so it's been marked as a duplicate, but reading that duplicate does not help at all. my definition and the definition in php.net are exactly the same! why do I still get this warning then? – Mike vG. Feb 12 '19 at 17:03

1 Answers1

-1

If you're declaration is:

PricingMySql::query($query)

And the actual method is:

mysqli::query ( string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )

Then you're missing the second parameter.

Try PricingMySql::query($query, null) if you haven't already.

This link provides more information: Declaration of Methods should be Compatible with Parent Methods in PHP

Adam
  • 1,294
  • 11
  • 24
  • if you look at my code above you'll see I am including the second parameter in my method. – Mike vG. Feb 12 '19 at 16:33
  • So you don't have a line of code doing this then: PricingMySql::query($query)? – Adam Feb 12 '19 at 16:34
  • You're passing one parameter, it takes two. – Adam Feb 12 '19 at 16:34
  • just adding a secondary paramenter on `query($query)` method to `query($query, $resultmode = NULL)` inside the `mysqli` class resolved the issue for me. – rc.adhikari Apr 19 '19 at 11:09
  • Indeed. Feel free to mark as the correct answer given that it was indeed missing the second parameter. – Adam Apr 21 '19 at 11:28