PHP 7.2.4 无法连接到我的 MySQL 8.0
PHP 7.2.4 unable to connect to my MySQL 8.0
这是我得到的错误...
Failed with mysqli_connect()
还有 new mysqli
如果我将此行放在注释“$conn = mysqli_connect...”中,我不会收到错误消息,因为它不会尝试连接到数据库。
Fatal error: Uncaught Error: Call to undefined function Classes\Connection\mysqli_connect() in C:\Apache24\htdocs\ControlePM\Classes\Connection\DBConnect.php:21 Stack trace: #0 C:\Apache24\htdocs\ControlePM\index.php(22): Classes\Connection\DBConnect->connect() #1 {main} thrown in C:\Apache24\htdocs\ControlePM\Classes\Connection\DBConnect.php on line 21
这是我的连接码
我尝试使用 new mysqli 同样的错误。
<?php
namespace Classes\Connection;
class DBConnect
{
var $host;
var $username;
var $password;
var $database;
public $dbc;
public $connInfo=FALSE;
public function __construct(){
$this->host = 'localhost:3306';
$this->username = 'root';
$this->password = 'MyPassWord';
$this->database = 'MyDB';
}
public function connect()
{
$conn = mysqli_connect($this->host,$this->username,$this->password,$this->database);
if (! $conn) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
mysqli_close($conn);
}
?>
Index.php
<?php
require_once ('Classes/Connection/DBConnect.php');
$conn = new DBConnect();
$conn->connect();
if (! $conn->connInfo){
$result = "Connection failed";
}
else $result = "Connection Succes";
?>
我错过了什么?
您正在使用命名空间,因此您需要指定它在全局命名空间 new \mysqli([...])
中查找 mysqli
,否则 PHP 将尝试查找 mysqli
在您当前的命名空间中(在您的 class 中指定的命名空间)。
其次,您可以通过 use
语句显式 'import' 它。
第三,检查是否安装了 mysqli 扩展的有用方法是执行以下 var_dump(function_exists('mysqli_connect'));
,由 this 回答提供。
PHP build
中缺少我的 mysqli
在 PHP.ini 文件中 "extension=mysqli" 被评论
这是我得到的错误...
Failed with mysqli_connect()
还有 new mysqli 如果我将此行放在注释“$conn = mysqli_connect...”中,我不会收到错误消息,因为它不会尝试连接到数据库。
Fatal error: Uncaught Error: Call to undefined function Classes\Connection\mysqli_connect() in C:\Apache24\htdocs\ControlePM\Classes\Connection\DBConnect.php:21 Stack trace: #0 C:\Apache24\htdocs\ControlePM\index.php(22): Classes\Connection\DBConnect->connect() #1 {main} thrown in C:\Apache24\htdocs\ControlePM\Classes\Connection\DBConnect.php on line 21
这是我的连接码
我尝试使用 new mysqli 同样的错误。
<?php
namespace Classes\Connection;
class DBConnect
{
var $host;
var $username;
var $password;
var $database;
public $dbc;
public $connInfo=FALSE;
public function __construct(){
$this->host = 'localhost:3306';
$this->username = 'root';
$this->password = 'MyPassWord';
$this->database = 'MyDB';
}
public function connect()
{
$conn = mysqli_connect($this->host,$this->username,$this->password,$this->database);
if (! $conn) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
mysqli_close($conn);
}
?>
Index.php
<?php
require_once ('Classes/Connection/DBConnect.php');
$conn = new DBConnect();
$conn->connect();
if (! $conn->connInfo){
$result = "Connection failed";
}
else $result = "Connection Succes";
?>
我错过了什么?
您正在使用命名空间,因此您需要指定它在全局命名空间 new \mysqli([...])
中查找 mysqli
,否则 PHP 将尝试查找 mysqli
在您当前的命名空间中(在您的 class 中指定的命名空间)。
其次,您可以通过 use
语句显式 'import' 它。
第三,检查是否安装了 mysqli 扩展的有用方法是执行以下 var_dump(function_exists('mysqli_connect'));
,由 this 回答提供。
PHP build
中缺少我的 mysqli在 PHP.ini 文件中 "extension=mysqli" 被评论