dplsnw7329 2014-12-03 15:38
浏览 120
已采纳

在html中使用php来显示数据库

I am trying to create a table in a HTML Document using php, and here is my code:

<p>Below is the table of all the books available: <br />

<table border="1" ><th>Book ID</th><th>Subject</th><th>Title</th><th>Print Edition</th><th>Condition</th><th>Asking Price</th>


<?php 
    $server= 'MYSERVER';
    $user = 'MYUSERNAME';
    $password = 'MYPASSWORD';
    $database = 'MYDBNAME';
    $mysqli = new mysqli('server', 'user', 'password', 'database');
    foreach ( $mysqli->query('SELECT user,host FROM mysql.user') as $row ) 
    {?>
    <tr><td><?php echo $res['ID'];?></td><td><?php echo $res['Subject'];?></td><td><?php echo $res['Title'];?></td><td><?php echo $res['PrintEdition'];?></td><td><?php echo $res['Cond'];?></td><td><?php echo $res['Price'];?></td></tr>
<?php } 
mysqli_close($mysqli);
?>
</table>

<br />
If you like to request any of these titles, Do the following:

. . . .

However, it seems I'm not sure what is happening, but I am having this as the outcome on my Chrome (this is the actual screen shot of my webpage):


Below is the table of all the books available: query('SELECT * FROM entry_database') as $row ) {?> Book ID Subject Title Print Edition Condition Asking Price

(The above row looks like a table)

If you like to request any of these titles, Do the following:

I'm not sure why this is happening, but I have a hunch that it is because HTML treats the -> as a tag. Is there any way of fixing this?

Just to explain, I have looked at all the answers, tried all the answers, and as far as I am aware, I can't find the answer.

Many thanks in advance.

UPDATE 1:

So I managed to sort out the additional code showing, thanks to @Fred -ii-

However, the data is not showing. I tried the following to output the date, but no success:

foreach ( $mysqli->query('SELECT * FROM entry_database') as $row ) 
    {?>
    <?php echo $row['ID'];?><br /><?php echo $row['Subject'];?><br /><?php echo $row['Title'];?><br /><?php echo $row['PrintEdition'];?><br /><?php echo $row['Cond'];?><br /><?php echo $row['Price'];?><br />
<?php } 
mysqli_close($mysqli);
?>

We are getting there, I am sure

  • 写回答

4条回答 默认 最新

  • donglu8344812 2014-12-03 15:44
    关注

    Ok, I'll make this an answer:

    Add $ to ('server', 'user', 'password', 'database') which should read as
    ($server, $user, $password, $database) otherwise PHP will throw you a message about "Undefined constants...".

    You need to make sure your file is .php which is why your page comes out looking that way, as code instead of being properly parsed.

    You can instruct Apache to treat .html files as PHP, but that's just more work.

    This I am highly suspecting is what you're doing, trying to run an .html file and expect it to run it as PHP just as a web browser would for HTML files.

    • If you're attempting to run this on your own computer, you will need to install a Webserver.

    Plus, you're doing as $row and echoing echo $res that should be as $res

    You're also only selecting the "user" and "host" columns from SELECT user,host and wanting to echo ID and Subject and Title and PrintEdition and Cond and Price which won't work.

    You need to either add/select all those columns, or do SELECT * and make absolutely sure that those columns do in fact exist in your table.


    Edit:

    Replace this block:

    foreach ( $mysqli->query('SELECT user,host FROM mysql.user') as $row ) {
        ?>
        <tr>
            <td><?php echo $row['ID'];?></td>
            <td><?php echo $row['Subject'];?></td>
            <td><?php echo $row['Title'];?></td>
            <td><?php echo $row['PrintEdition'];?></td>
            <td><?php echo $row['Cond'];?></td>
            <td><?php echo $row['Price'];?></td>
        </tr>
    

    with:

    $results = $mysqli->query('SELECT * FROM entry_database');
    
    while($row = $results->fetch_assoc()) {
        ?>
        <tr>
            <td><?php echo $row['ID'];?></td>
            <td><?php echo $row['Subject'];?></td>
            <td><?php echo $row['Title'];?></td>
            <td><?php echo $row['PrintEdition'];?></td>
            <td><?php echo $row['Cond'];?></td>
            <td><?php echo $row['Price'];?></td>
        </tr>
    

    or:

    <!DOCTYPE html>
    <head></head>
    <body>
        <table style="width: 100%;" border="1" cellspacing="5" cellpadding="5">
            <thead>
                <tr>
                <th align="left" valign="middle">ID</th>
                <th align="center" valign="middle">Subject</th>
                <th align="center" valign="middle">Title</th> 
    
                <th align="left" valign="middle">PrintEdition</th>
                <th align="center" valign="middle">Cond</th>
                <th align="center" valign="middle">Price</th>               
            </tr>
        </thead>
        <tbody>
        <?php 
        $server= 'MYSERVER';
        $user = 'MYUSERNAME';
        $password = 'MYPASSWORD';
        $database = 'MYDBNAME';
        $mysqli = new mysqli($server, $user, $password, $database);
        $results = $mysqli->query('SELECT * FROM entry_database');
        while($row = $results->fetch_assoc()): ?>
            <tr>
                <td align="center" valign="middle"><?php echo $row['ID']; ?></td>
                <td align="center" valign="middle"><?php echo $row['Subject']; ?></td>
                <td align="left" valign="middle"><?php echo $row['Title']; ?></td>
                <td align="center" valign="middle"><?php echo $row['PrintEdition']; ?></td>
                <td align="center" valign="middle"><?php echo $row['Cond']; ?></td>
                <td align="left" valign="middle"><?php echo $row['Price']; ?></td>
            </tr>
        <?php endwhile;?>
        </tbody>
    </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大