user2584481
user2584481

Reputation: 53

SQL table data not appearing on page

simple one, I am having difficulty with my sql data not showing on webpage using php. I have a seperate page which connects to the DB as per. However using simple connection php scripting is not displaying the results...

<?php
include("header.php");
include("connect.php");
?>



<?php

    $sql = mysqli_query("SELECT * FROM members ORDER BY id ASC");

    $id = 'id';
    $username = 'username';
    $useremail = 'useremail';
    $rows = mysqli_fetch_assoc($sql);

    echo 'Name: ' . $rows[$id] . '<br />' . 'Username: ' . $rows[$useremail];

    ?>

I have even tried this but still nothing...

<?php

    include 'includes/connect.php';


    $query = "SELECT * FROM members";

    $result = mysqli_query ($query);

    while($person = mysqli_fetch_array($result)) {

        echo "<h1>" . $person['useremail'] . "</h1>";

    }

    ?>

Upvotes: 0

Views: 671

Answers (1)

mahendra
mahendra

Reputation: 133

Please check you have written all connection variables correctly in connect.php. You are using $rows[$id] which is not problem because you have defined variable $id='id'. But problem is you are not using loop. If you will print

$rows = mysqli_fetch_assoc($sql);
echo $rows[$id];

it will just give one record; Please try with following

<?php
$host='localhost';
$user='root';
$pass='';
$db='mydata';//change above four variable as per your setting or write down in connect.php
$con=mysqli_connect($host,$user,$pass) or die(mysqli_error());
mysqli_select_db($con,$db);
$id = 'id';
$username = 'username';
$useremail = 'useremail';
$q="SELECT * FROM members ORDER BY id ASC";
$r=mysqli_query($con,$q);
while($rows=mysqli_fetch_assoc($r)){
    echo "Name :".$rows[$username]."<br />"."useremail :".$useremail."<br />";
}
?>

Upvotes: 1

Related Questions