0

I am inputting data into MySQL tables using PHP forms and displaying the table when requested (this must be done using MySQLi).

I managed to insert the data without a problem, but I am having trouble displaying the table using MySQLi and PHP. I need to display the results in an XHTML table.

I tried to follow tutorials I found online, but they dont seem to work; my current code displays the header, and then a blank row beneath it instead of the data in my table.

I know it connect and like I said, it is able to insert. Could someone please show me (and explain please) how I would solve my issue?

                $query = "select * from $table_name;";
                if ($result = mysqli_query($db_link, $query)){

                    echo "<table>";
                    //header
                    echo "<tr><td>Date Added</td>";
                            echo "<td>Name</td>";
                            echo "<td>Email</td>";
                    echo "<td>Gender</td>";
                                echo "<td>Country</td>";
                    echo "<td>Subject</td>";
                            echo "<td>Comment</td>";
                    echo "<td>Subscription</td></tr>";
                        //data  
                         while ($row = $result->fetch_row())  {
                        $Row = mysqli_fetch_assoc($result);
                                echo "<tr><td>{$Row[0]}</td>";
                                echo "<td>{$Row[1]}</td>";
                                echo "<td>{$Row[2]}</td>";
                        echo "<td>{$Row[3]}</td>";
                                echo "<td>{$Row[4]}</td>";
                        echo "<td>{$Row[5]}</td>";
                                echo "<td>{$Row[6]}</td>";
                        echo "<td>{$Row[7]}</td></tr>";
                        } 

                        echo "</table>";
                }

                mysqli_free_result($result);
                mysqli_close($db_link);
AstroCB
  • 12,337
  • 20
  • 57
  • 73
ShadowViper
  • 365
  • 1
  • 5
  • 24

3 Answers3

1

Try mysqli_fetch_array()

            $query = "select * from $table_name;";
            if ($result = mysqli_query($db_link, $query)){

                echo "<table>";
                //header
                echo "<tr><td>Date Added</td>";
                        echo "<td>Name</td>";
                        echo "<td>Email</td>";
                echo "<td>Gender</td>";
                            echo "<td>Country</td>";
                echo "<td>Subject</td>";
                        echo "<td>Comment</td>";
                echo "<td>Subscription</td></tr>";
                    //data  
                     while ($row = mysqli_fetch_array($result))  {
                      echo "<tr><td>{$row[0]}</td>";
                      echo "<td>{$row[1]}</td>";
                      echo "<td>{$row[2]}</td>";
                      echo "<td>{$row[3]}</td>";
                      echo "<td>{$row[4]}</td>";
                      echo "<td>{$row[5]}</td>";
                      echo "<td>{$row[6]}</td>";
                      echo "<td>{$row[7]}</td></tr>";
                    } 

                    echo "</table>";
            }

            mysqli_free_result($result);
            mysqli_close($db_link);
Partha Mitra
  • 130
  • 7
  • Thank you that worked, but what is the different between mysqli_fetch_array and mysqli_fetch_assoc? – ShadowViper Mar 02 '15 at 04:46
  • mysql_fetch_assoc This function will return a row as an associative array where the column names will be the keys storing corresponding value. mysql_fetch_array This function will actually return an array with both the contents of mysql_fetch_rowand mysql_fetch_assoc merged into one. It will both have numeric and string keys which will let you access your data in whatever way you'd find easiest. – Partha Mitra Mar 02 '15 at 04:56
0

You have to escape you php

Echo "<td>".$row[6]."</td>";

http://www.w3schools.com/php/php_mysql_select.asp

Creaven
  • 319
  • 2
  • 16
0

Hope it works

    while($row = $result->fetch_assoc()) 
    {
            echo "<tr>";
            echo "<td>". $row["DateAdded"]."</td>";
            echo "<td>". $row["Name"]."</td>";
            echo "<td>".$row["Email"] ."</td>";
            echo "<td>".$row["Gender"] ."</td>";
            echo "<td>".$row["Country"] ."</td>";
            echo "<td>".$row["Subject"] ."</td>";
            echo "<td>".$row["Comment"] ."</td>";
            echo "<td>".$row["Subscription"] ."</td>";
            echo "</tr>";

    }
Sivasailanathan
  • 135
  • 2
  • 11