MySQLi查询错误无法获取mysqli

时间:2018-01-14 00:54:37

标签: php mysqli

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

错误: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;

有人可以帮我这个吗?非常感谢!

2 个答案:

答案 0 :(得分:0)

解决方案:

我改变了第4行:

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

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

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

答案 1 :(得分:-1)

我刚刚在我的服务器上试过它而且我没有看到它有什么问题。只需确保您的登录详细信息以及数据库和表名称都是正确的。

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

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

?>