如何在我的html页面中显示我的数据库的值?

时间:2015-05-21 06:48:04

标签: php html mysql

我想在我的html页面中显示我的数据库中列的值

现在我有连接,回显值显示

但我希望它能在我的HTML中显示,我该如何解决?

这是我的代码:

<!doctype html>
<html>
<head></head>

<body>

<?php
$dbhost = 'localhost';
$dbuser = '';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);

if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT 8krt, 14krt, 18krt FROM orders';

mysql_select_db('my_database');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_assoc($retval))
{
    echo "First14 : {$row['14krt']}  <br> ".
         "Second18 : :" . $row["18krt"] . "  <br> ";
} 

mysql_close($conn);
?>

 <div class="divWrapper">
        <table cellspacing="0" cellpadding="0" class="tblForm">
            <tr>
                <td>HERE I WANT TO SHOW THE VALUE OF "14krt" from the database</td>
             <td>HERE I WANT TO SHOW THE VALUE OF "18krt" from the database</td>
           </tr>

            <div>

4 个答案:

答案 0 :(得分:0)

您有范围问题。将$value设为全局变量,如下所示:

    $value;   
     while($row = mysql_fetch_assoc($retval))
        {
            $value = $row['14krt'];
        } 

    <td><?php echo $value; ?></td>

如果$row['14krt']不止一次,请使用数组。

$value=array();   
while($row = mysql_fetch_assoc($retval))
{
   $value[] = $row['14krt'];
} 

<?php for($i=0; $i<count($value); $i++) { ?>
<td><?php echo $value[$i] ?></td>
<?php } ?>

让我知道这是否有效。

答案 1 :(得分:0)

试试这个

<!doctype html>
<html>
<head>
</head>
<body>
    <?php
    $dbhost = 'localhost';
    $dbuser = '';
    $dbpass = '';
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);

    if(! $conn )
    {
        die('Could not connect: ' . mysql_error());
    }
    $sql = 'SELECT 8krt, 14krt, 18krt FROM orders';

    mysql_select_db('my_database');
    $retval = mysql_query( $sql, $conn );
    if(! $retval )
    {
        die('Could not get data: ' . mysql_error());
    }
    ?>
    <div class="divWrapper">
        <?php
if (mysql_num_rows($retval)) {  ?>
        <table cellspacing="0" cellpadding="0" class="tblForm">
            <?php

            while($row = mysql_fetch_assoc($retval))
   { ?>

            <tr>
                <td><?php echo $row['14krt'];?></td>
                <td><?php echo $row['18krt'];?></td>

            </tr>
            <?php
   }
   echo "</table>";
}
mysql_close($conn);
?>
            </div>

答案 2 :(得分:0)

您还需要TD周围的while

<?php 
while($row = mysql_fetch_assoc($retval))
{
   <td><?php $row['14krt'] ?></td>
   <td><?php $row["18krt"] ?></td>
}

答案 3 :(得分:0)

尝试在table元素

中使用while / endwhile
<div class="divWrapper">

<table cellspacing="0" cellpadding="0" class="tblForm">
<? while ($row = mysql_fetch_assoc($retval)): ?>
    <tr>
        <td><?php echo $row['14krt']; ?></td>
        <td><?php echo $row['14krt']; ?></td>
    </tr>
<? endwhile; ?>
</div>