简体   繁体   English

使用MySQLi在PHP中显示查询结果

[英]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). 我正在使用PHP表单将数据输入到MySQL表中,并在请求时显示该表(必须使用MySQLi完成)。

I managed to insert the data without a problem, but I am having trouble displaying the table using MySQLi and PHP. 我设法毫无问题地插入了数据,但是在使用MySQLi和PHP显示表时遇到了问题。 I need to display the results in an XHTML table. 我需要在XHTML表中显示结果。

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

Try mysqli_fetch_array() 试试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);

You have to escape you php 你必须逃脱你的PHP

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

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

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

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM