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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘