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

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?

图片转代码服务由CSDN问答提供 功能建议

这是我得到的错误......

mysqli_connect()

以及 new mysqli

 致命错误:未捕获错误:在C:\ Apache24 \ htdocs \ ControlePM \ Classes \ Connection \ DBConnect.php中调用未定义的函数Classes \ Connection \ mysqli_connect():21 堆栈跟踪:#0 C:\ Apache24 \ htdocs \ ControlePM \ index.php(22):在C:\ Apache24 \ htdocs \ ControlePM \ Classes \中抛出Classes \ Connection \ DBConnect-&gt; connect()#1 {main} 第21行的连接\ DBConnect.php 
   
 
 

这是我的连接代码

我尝试使用 new mysqli < / strong>相同的错误。

 &lt;?php 
 namespace Classes \ Connection; 
 class DBConnect 
 {
 var $ host; 
 var $ username;  
 var $ password; 
 var $ database; 
 public $ dbc; 
 public $ connInfo = FALSE; 
 
 public function __construct(){
 $ this-&gt; host ='localhost:3306'  ; 
 $ this-&gt; username ='root'; 
 $ this-&gt; password ='MyPassWord'; 
 $ this-&gt; database ='MyDB'; 
} 
公共函数connect()  
 {
 $ conn = mysqli_connect($ this-&gt; host,$ t 他的 - &GT;用户名,$这 - &GT;密码,$这 - &GT;数据库);  
 
 if if(!$ conn){
 echo“错误:无法连接到MySQL。”  。  PHP_EOL; 
 echo“Debugging errno:”。  mysqli_connect_errno()。  PHP_EOL; 
 echo“调试错误:”。  mysqli_connect_error()。  PHP_EOL; 
退出; 
} 
 echo“成功:与MySQL建立了正确的连接!my_db数据库很棒。”  。  PHP_EOL; 
 echo“主机信息:”。  mysqli_get_host_info($ link)。  PHP_EOL; 
 mysqli_close($ conn); 
} 
?&gt; 
   
 
 

Index.php \ n
 &lt;?php 
require_once('Classes / Connection / DBConnect.php');  
 $ conn = new DBConnect(); 
 $ conn-&gt; connect(); 
 
if(!$ conn-&gt; connInfo){
 $ result =“Connection failed”; 
} 
else  $ result =“Connection Succes”; 
?&gt; 
   
 
 

我缺少什么?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongying7667 2018-05-28 18:27
    已采纳

    My mysqli was missing from the PHP build

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

    点赞 评论
  • dqybeh2884 2018-04-26 16:34

    You are using a namespace therefore you'll need to specify that it look for mysqli in the global namespace new \mysqli([...]) otherwise PHP will try and look for mysqli in your current namespace (the one specified in your class).

    Secondly, you could explicitly 'import' it via the use statement.

    Thirdly, a useful way to check if the mysqli extension is installed is by executing the following var_dump(function_exists('mysqli_connect'));, courtesy of this answer.

    点赞 评论

相关推荐 更多相似问题