简体   繁体   中英

Returning results from database

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'; ?> <?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>";
?>

you didnt select your database

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

this require another parameter which is your db name

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM