首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MySQLi查询错误无法获取mysqli

MySQLi查询错误无法获取mysqli
EN

Stack Overflow用户
提问于 2018-01-14 08:54:37
回答 2查看 2K关注 0票数 0

当我尝试下面的代码时,我得到了以下错误:

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

我使用的代码..

代码语言:javascript
复制
<?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行:

代码语言:javascript
复制
$mysqli = NEW MySQLi("localhost","root","","dbproject");

第7行:

代码语言:javascript
复制
$resultSet = $mysqli->query("SELECT * FROM clients");

第23行:

代码语言:javascript
复制
    echo $mysqli->error;

有没有人能帮我一下?非常感谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-01-14 09:02:38

解决方案:

我将第4行代码从:

代码语言:javascript
复制
$mysqli = NEW MySQLi("localhost","root","","dbproject");

代码语言:javascript
复制
$mysqli = NEW MySQLi("localhost:3307","root","","dbproject");

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

票数 0
EN

Stack Overflow用户

发布于 2018-01-14 09:06:59

我刚刚在我的服务器上试过了,我看不出有什么问题。只需确保登录详细信息与数据库和表名一样正确即可。

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

代码语言:javascript
复制
<?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;
}

?>
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48245769

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档