duanfei8897 2018-04-26 16:27
浏览 532
已采纳

PHP 7.2.4无法连接到我的MySQL 8.0

This is the error I get...

Failed with mysqli_connect()

and also with new mysqli If I put this line in comment "$conn = mysqli_connect..." I don't get the error since it doesn't try to connect to the DB.

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

This is my connection code

I tried with new mysqli same error.

<?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";
?>

What am I missing?

  • 写回答

2条回答 默认 最新

  • dongying7667 2018-05-28 18:27
    关注

    My mysqli was missing from the PHP build

    In the PHP.ini file "extension=mysqli" was commented

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵