0

I am trying to display and later amend the data inserted into this database on my web page. But the data is not displaying. I corrected my mistakes from my previous post and tried to troubleshoot. I believe the connection to the database is working because I am able to insert data into the database.

 /*top of page invites.php*/
<?php 
include("LoginRegistrationSystem/connect.php");
include("guestlist.php");
include("functions.php");
if(logged_in())
{

?>

    /*in invites.php*/

   <?php
  $sql = "SELECT fname, lname, email, contact  FROM guestlist";
  $resultset = mysqli_query($con, $sql) or die("database error:". 
mysqli_error($con));

  echo "<table border="2">
        <tr>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Contact</th>
      <th>Invitation</th>
    </tr>";

  while($row = mysqli_fetch_array($resultset)) 
   { 

        echo "<tr>";
          echo"<td>" . $row['fname'] . "</td>";
          echo"<td>" . $row['lname']. "</td>";
          echo"<td>" . $row['email']. "</td>";
          echo"<td>" . $row['contact']. "</td>";
        echo "<tr>";
}
        echo "</table>";
        mysqli_close($con);
 ?> 



/*LoginRegistrationSystem/connect.php*/
    <?php

    $con = mysqli_connect("localhost","root","","registration");

    if(mysqli_connect_errno())
    {
        echo "Error occured while connecting with database 
".mysqli_connect_errno();
        }

    session_start();
?>
halfer
  • 19,824
  • 17
  • 99
  • 186
alexvin
  • 41
  • 7

2 Answers2

1

There is one major error I'm spotting. Near the top of the code, you have a conditional:

if(logged_in()) {

but no closing brace to match it. If your page isn't loading at all, this is likely the problem.

If you fix this and the table still doesn't display, make sure that you have permissions to SELECT on the guestlist table. It would be odd, considering that you can INSERT into the database, but check it nonetheless. Permissions errors been a huge cause of headache for me in the past.

As a bit of friendly advice, you might also want to consider switching from mysqli_fetch_array to mysqli_fetch_assoc. While this will not change how your program functions for the purposes here, with mysqli_fetch_array the results are indexed both numerically and associatively, i.e. there is both a $row[0] and a $row['fname'] even though they are the same thing. mysqli_fetch_assoc returns only the associative array, i.e. there are no numerical indices in the return value.

Also, you should indent your code properly. It will drastically improve readability and help you with debugging.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Hi. I forgot to mention I ended with else part for the top. Thank you for clarifying my doubt with clear explanation of assoc and array. How can I fix the permission error? – alexvin Sep 13 '17 at 00:48
  • @alexvin The general SQL syntax is `GRANT [permission] ON [object] TO [username]`. Full documentation for the `GRANT` command can be found at https://learn.microsoft.com/en-us/sql/t-sql/statements/grant-object-permissions-transact-sql. MySQL Workbench also has a nice GUI for this. – public satanic void Sep 13 '17 at 01:15
0

Try this: while($row = mysqli_fetch_assoc($resultset)){ .... }