php - Fatal error: Uncaught ArgumentCountError: mysqli_connect_errno() expects exactly 0 arguments, 1 given
I received the following error on my localhost:
Fatal error: Uncaught ArgumentCountError: mysqli_connect_errno() expects exactly 0 arguments, 1 given in
Using:
XAMPP v3.2.4, both Apache and MySQL running on correct ports (they are active). It is giving an error on line 52, which is the if
statement below.
Code:
public function __construct()
{
$db = $this->localDatabase();
$this->con = mysqli_connect($db['host'], $db['user'], $db['pass'], $db['db']);
if (mysqli_connect_errno($this->con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
}
Answer
Solution:
The function mysqli_connect_errno
does not take any parameters (https://www.php.net/manual/en/mysqli.connect-errno.php), the purpose of this function is to:
Returns the last error code number from the last call to mysqli_connect().
If you are looking to determine if the connection successfuly established then you should do the following:
public function __construct()
{
$db = $this->localDatabase();
$this->con = mysqli_connect($db['host'], $db['user'], $db['pass'], $db['db']);
if (!$this->con)
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
}
You can see an example on php.net (under Example #1): https://www.php.net/manual/en/function.mysqli-connect.php
Answer
Solution:
The function mysqli_connect_errno
doesn't take any arguments as can be seen in the documentation.
You really should not be using this function! Please, forget that it even exists. Instead enable proper mysqli error reporting and stop worrying about manual error checking.
The correct way to open a connection with mysqli is this:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$this->con = new mysqli($db['host'], $db['user'], $db['pass'], $db['db']);
$this->con->set_charset('utf8mb4');
Nothing more!
Source