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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题