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);
2
  • 2
    What is the error you are getting?? Commented Mar 2, 2015 at 4:47
  • non thats the thing. Commented Mar 2, 2015 at 4:53

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you that worked, but what is the different between mysqli_fetch_array and mysqli_fetch_assoc?
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.
0

You have to escape you php

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

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

2 Comments

thats not the full code, i do escape php at the end.
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>";

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.