如何在html中显示数据库中的数据

时间:2014-11-22 16:24:33

标签: php html database mysqli html-table

我正在尝试从我管理的数据库中提取信息,并将其存储在表格中。但是我一直都会遇到错误。有谁能看到这个问题?

<?php
//Make connection to database
$connection = mysqli_connect('localhost', 'root', '', 'c3438525');
//Display heading

//run query to select all records from customer table
//store the result of the query in a variable called $result

//Use a while loop to iterate through your $result array and display
//each field wrapped in appropriate HTML table tags.



//include 'connection.php'  

include 'products.css'
$query="SELECT * FROM products";

$results=mysqli_query($connection, $query);

while($row=mysqli_fetch_assoc($results)){

    ?>



<table class='display'>
<tr><td><?php echo "<img src=$rows[$product_image] height='200px' width='200px'>"  ?></td></tr>

<tr>
<td><b><?php echo "$rows[$product_name]" ?></td>
<td><b><?php echo "Price: £ $rows[$price]" ?></td>
<td><b><?php echo "Picture: $rows[$product_image]" ?></td>

</tr>


</table>
    <?php   
}
?>

2 个答案:

答案 0 :(得分:1)

您需要将数组键作为字符串提供,并使用分号终止语句。

<table class="display">
<tr>
    <td>
    <?php
       // you can also write an array value directly into a string
       echo "<img src=\"{$rows['product_image']}\" height=\"200px\" width=\"200px\">";
    ?>
    </td>
</tr>
<tr>
    <td><b><?php echo $rows['product_name']; ?></b></td>
    <td><b><?php echo "Price: £ " . $rows['price']; ?></b></td>
    <td><b><?php echo "Picture: " . $rows['product_image']; ?></b></td>
</tr>
</table>

此外,您应关闭那些</b>代码(但<strong>代码优先于<b>

最好在文档的<head>中包含样式

<head>
    <link rel="stylesheet" type="text/css" href="products.css">
</head>

但是include 'products.css';会起作用(不能忘记那些半纤维素)

答案 1 :(得分:0)

此处img src正在考虑字符串不变量。 echo末尾没有终止半克隆。只需改变它。

<table class='display'>
    <tr><td><?php echo '<img src="'.$rows[$product_image].'" height="200px" width="200px">';  ?></td></tr>
                                 //^^^^^^^^^^^^^^^^^^^^^^^^//
    <tr>
        <td><b><?php echo "$rows[$product_name]"; ?></td>
        <td><b><?php echo "Price: £ $rows[$price]"; ?></td>
        <td><b><?php echo "Picture: $rows[$product_image]"; ?></td>
    </tr>
</table>

同时建议您<link rel="stylesheet" type="text/css" href="products.css">代替include 'products.css'