ShadowViper
ShadowViper

Reputation: 365

Using MySQLi to display results from query in PHP

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);

Upvotes: 0

Views: 5342

Answers (3)

Sivasailanathan
Sivasailanathan

Reputation: 135

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

    }

Upvotes: 0

Partha Mitra
Partha Mitra

Reputation: 130

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);

Upvotes: 1

Creaven
Creaven

Reputation: 319

You have to escape you php

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

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

Upvotes: 0

Related Questions