doy57007 2015-04-21 10:12
浏览 38
已采纳

从数据库返回结果

I am trying to do a simple SELECT to return rows of data from my database. I have a valid connection from my database so I know the issue is not there. I have ensured the names of each column are correct but it just returns 0 results.

My table inside the db is called 'user' and here is the members.php file:

<?php include 'header.php'; ?> <- this is where the db conect file is pulled in.

    <?php

        $sql = "SELECT id, username, email_address FROM user";
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {

            // output data of each row
            while($row = $result->fetch_assoc()) {
            echo 
                $row["id"];
            }

        } else {
            echo "0 Members";
        }

        $conn->close();
    ?>

Just for ref here is my DB connection (Not the most secure i am just testing):

<?php
$servername = "localhost";
$username = "***********";
$password = "**********";

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

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
    echo "Connected successfully<br><br>";
?>
  • 写回答

1条回答 默认 最新

  • douwen5546 2015-04-21 10:16
    关注

    you didnt select your database

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

    this require another parameter which is your d.b name

    $conn=new mysqli($servername, $username, $password,$db_name);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比