Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我无法从数据库中获得任何数据输出,我只得到“0结果”作为else语句,是什么情况?_Php_Mysql_Sql_Wampserver - Fatal编程技术网

Php 我无法从数据库中获得任何数据输出,我只得到“0结果”作为else语句,是什么情况?

Php 我无法从数据库中获得任何数据输出,我只得到“0结果”作为else语句,是什么情况?,php,mysql,sql,wampserver,Php,Mysql,Sql,Wampserver,我在用php代码获取一些数据库信息时遇到了一些问题。 我得到的只是一条信息:已成功连接0个结果。 这是我的代码员,提前谢谢你的帮助 <?php $servername = "example"; $username = "example1"; $password = "example2"; $row = array(); $conn = new mysqli($servername,$username,$password); if ($c

我在用php代码获取一些数据库信息时遇到了一些问题。 我得到的只是一条信息:已成功连接0个结果。 这是我的代码员,提前谢谢你的帮助

 <?php
    $servername = "example";
    $username = "example1";
    $password = "example2";
    $row = array();

    $conn = new mysqli($servername,$username,$password);

    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    echo "Connected successfully";
    $sql = "Select Distinct subject from mobile_math_science_toc";
    $result = mysqli_query($conn, $sql);
    if ($result = $conn->query($sql)) {
        // output data of each row
        while($row = mysqli_fetch_assoc($result)) {
            echo "Subject " ,$row["subject"];
        }
    } else {
        echo "0 results ";
    }

    mysqli_close($conn);
    ?>
改变

试一试


在检查是否获得数据时

您应该在创建数据库连接时添加dbname。您可以使用mysqli_num_rows函数来计算行数

<?php
    $servername = "example";
    $username = "example1";
    $password = "example2";
    $dbname = "your_db_name"; // Specify your db-name here.

    $conn = new mysqli($servername,$username,$password,$dbname);

    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    echo "Connected successfully";

    $sql = "Select Distinct subject from mobile_math_science_toc";
    $result = mysqli_query($conn, $sql);
    // Checking if there are some records available.
    if (mysqli_num_rows($result) > 0) {
        // output data of each row
        while($row = mysqli_fetch_assoc($result)) {
            echo "Subject " ,$row["subject"];
        }
    } else {
        echo "0 results ";
    }

    mysqli_close($conn);
?>

忘记选择数据库!!没有按他们说的选择数据库。我也很好奇你为什么要两次打电话问这个问题?一旦使用了过程式风格,然后你又在第二行中使用了它,写下第一个结果?@MagnusEriksson我对php不熟悉,这是我的第一天,所以我正在尝试找到解决方法:P,任何帮助都会很好,谢谢你注意到查询问题。如果你是新手,你应该学习手册:一个好的答案总会有一个解释,说明做了什么以及为什么这样做,不仅是为了OP,而且是为了将来的访客,以便他们可以找到这个问题并阅读你的答案。@RiggsFolly:当然。谢谢你的建议。前面我在代码中添加了注释。现在,我在代码之前也添加了简短的描述。希望现在看起来不错。@KeyurPanchal是的,它起作用了,我试过你说的,我从数据库中得到了正确的信息。对不起,昨天没有回复。我病了,不能上班了。谢谢你的帮助,上帝保佑你。
 $conn = new mysqli($servername,$username,$password, "<your database name>");
    $result = mysqli_query($conn, $sql);
        if ($result = $conn->query($sql)) {
}
    $result = $conn->query($sql);
        if ($result) {
}
    if (mysqli_num_rows($result) > 0) {
<?php
    $servername = "example";
    $username = "example1";
    $password = "example2";
    $dbname = "your_db_name"; // Specify your db-name here.

    $conn = new mysqli($servername,$username,$password,$dbname);

    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    echo "Connected successfully";

    $sql = "Select Distinct subject from mobile_math_science_toc";
    $result = mysqli_query($conn, $sql);
    // Checking if there are some records available.
    if (mysqli_num_rows($result) > 0) {
        // output data of each row
        while($row = mysqli_fetch_assoc($result)) {
            echo "Subject " ,$row["subject"];
        }
    } else {
        echo "0 results ";
    }

    mysqli_close($conn);
?>