【问题标题】:MySQLi Query error Couldn't fetch mysqliMySQLi 查询错误无法获取 mysqli
【发布时间】:2018-01-14 00:54:37
【问题描述】:

我在尝试下面的代码时收到以下错误:

错误:Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in /volume1/web/index.php on line 4 Warning: mysqli::query(): Couldn't fetch mysqli in /volume1/web/index.php on line 7 Warning: main(): Couldn't fetch mysqli in /volume1/web/index.php on line 23

我正在使用的代码..

<?php

// Connect to the DB
$mysqli = NEW MySQLi("localhost","root","","dbproject");

//Query the DB
$resultSet = $mysqli->query("SELECT * FROM clients");

// Count the returned rows
if($resultSet->num_rows != 0) {
    while($rows = $resultSet->fetch_assoc())
    {
        $id = $rows['id'];
        $fname = $rows['firstname'];
        $lname = $rows['lastname'];
        $country = $rows['country'];

        echo "<p>ID: " .$id. " <br /> Name: ".$fname." ".$lname. "<br /> Country: ".$country." </p>"; 
    }
// Turn the results into an array
// Display the results
} else{
    echo $mysqli->error;
}

?>

由于错误涉及第 4、7 和 23 行

第 4 行:

$mysqli = NEW MySQLi("localhost","root","","dbproject");

第 7 行:

$resultSet = $mysqli->query("SELECT * FROM clients");

第 23 行:

    echo $mysqli->error;

有人可以帮我解决这个问题吗?非常感谢!

【问题讨论】:

    标签: php mysqli


    【解决方案1】:

    解决办法:

    我将第 4 行更改为:

    $mysqli = NEW MySQLi("localhost","root","","dbproject");
    

    到

    $mysqli = NEW MySQLi("localhost:3307","root","","dbproject");
    

    添加端口以某种方式修复它。

    【讨论】:

    • 3307 端口在您的数据库服务器的配置文件(my.cnf、my.ini 或类似文件)中定义。 mysql默认是3306。
    【解决方案2】:

    我刚刚在我的服务器上尝试过,我没有发现任何问题。只需确保您的登录详细信息以及数据库和表名正确无误。

    这是我刚刚在我的服务器上测试的脚本。我添加了一个连接测试并更改了数据库和表名:

    <?php
    
    // Connect to the DB
    $mysqli = NEW MySQLi("localhost","root","","test");
    
    // Check connection
    if ($mysqli->connect_error) {
        die("Connection failed: " . $mysqli->connect_error);
    } 
    
    //Query the DB
    $resultSet = $mysqli->query("SELECT * FROM testing");
    
    // Count the returned rows
    if($resultSet->num_rows != 0) {
        while($rows = $resultSet->fetch_assoc())
        {
            $id = $rows['ID'];
    
    
            echo "<p>ID: " .$id. " </p>"; 
        }
    // Turn the results into an array
    // Display the results
    } else{
        echo $mysqli->error;
    }
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-25
      • 1970-01-01
      • 2018-09-14
      相关资源
      最近更新 更多