您好,我正在尝试将Mysql表中的项目回显到html表中,这是我的代码:

    <?php
        $host="localhost";
        $username="root";
        $password="";
        $db_name="content_management";
        $tbl_name="houses";

        mysql_connect("$host", "$username", "$password")or die("cannot connect");
        mysql_select_db("$db_name")or die("cannot select DB");

        $sql = "SELECT * FROM $tbl_name";
        $result=mysql_query($sql);

        while ( $row = mysql_fetch_assoc($result) ) {
            echo "<img src='" . $row['picture'] . "'>";
                echo $row['price'];
                echo $row['type'];
            echo $row['location'];
            echo $row['description'];
        }
    ?>


我想回应一下房屋的图片,价格,类型,位置和描述。

我尝试了各种方法(除非我在某个地方搞砸,否则其中大多数方法都不会起作用)您能给我一些建议吗?

最佳答案

<?php

echo item();

function item()
 {
    $html='';
    $host="localhost";
    $username="root";
    $password="";
    $db_name="content_management";
    $tbl_name="houses";

    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");

    $sql = "SELECT * FROM $tbl_name";
    $result=mysql_query($sql);

    $html .="<table>";

   while ( $row = mysql_fetch_assoc($result) )
   {

       $html .="<tr><td><img src='" . $row['picture'] . "'></td>";
       $html .="<td>".$row['price']."</td>";
       $html .="<td>".$row['type']."</td>";
       $html .="<td>".$row['location']."</td>";
       $html .="<td>".$row['description']."</td></tr>";

    }
 $html .="</table>";

return $html;
}
?>

关于php - 如何将MySQL中的项目回显到html表中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30226357/

10-11 02:54