想要将表行数据显示为html中的标题

时间:2016-07-20 06:42:49

标签: php html mysql

我有数据库名称" recipee"和表名"内容"在里面。在"内容"有两列名为" id"和"列表"。我想显示" list"作为标题在html中。表"内容"示例

**id    list**
  1   Malai Paneer
  2   Kadhai Paneer 

所以,我想展示" Malai Paneer"和" Kadhai Paneer"作为html中的标题。我正在使用mysql,php和html。

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
//Open a new connection to the MySQL server
$mysqli = new mysqli('localhost','root','','recipee');

//Output any connection error
if ($mysqli->connect_error) {
    die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}

$content_id = (int)$_GET['id'];

//MySqli Select Query
$results = $mysqli->query("SELECT id, list FROM contents where id = $content_id");
$id = ["id"];
$list = ["list"];



// Frees the memory associated with a result
$results->free();

// close connection 
$mysqli->close();
?>
<h1> <?php echo $list?> </h1>
</body>
</html>

预期的输出结构应该是:

*********************** Kadhai Paneer(作为主页标题)**************** * * * * * * * *

3 个答案:

答案 0 :(得分:0)

很难理解具体要求是什么。不过这里有一些答案:

  1. 以逗号分隔标题显示所有数据。
  2. 例如:

    Malai Paneer,Kadhai Paneer

    <html>
        <head>
            <title>Last 10 Results</title>
        </head>
        <body>
            <h1>
                <?php
                    $connect = mysql_connect("localhost","root", "root");
                    if (!$connect) {
                        die(mysql_error());
                    }
                    mysql_select_db("recipee");
                    $results = mysql_query("SELECT * FROM contents");
                    while($row = mysql_fetch_array($results)) {
                    ?>
    
                            <?php echo $row['list']?>, 
    
                    <?php
                    }
                    ?>
            </h1>
        </body>
    </html>
    

答案 1 :(得分:0)

使用此

<html>
    <head>
        <title>Last 10 Results</title>
    </head>
    <body>
        <table border="1px solid black">

        <tbody>
        <tr>
        <?php
            $connect = mysqli_connect("localhost","root", "root","recipee");
            if (!$connect) {
                die(mysqli_error($connect));
            }

            $results = mysqli_query($connect,"SELECT list FROM contents");
            while($row = mysqli_fetch_array($results)) {
            ?>

                    <th><?php echo $row['list']?></th>



            <?php
            }
            ?>
            </tr>
            </tbody>
            </table>
    </body>
</html>

答案 2 :(得分:0)

我认为使用

从数据库中分配获取的数据
null

不对。

让我们使用prepared statement方法获取您的数据:

$id = ["id"];
$list = ["list"];

然后,我们可以通过以下方式回应结果:

$results = $mysqli->query("SELECT id, list FROM contents where id = ?"); /* PREPARE YOUR QUERY */
$results->bind_param("i", $content_id); /* THIS WILL BIND THE VARIABLE TO YOUR QUERY ABOVE */
$results->execute(); /* EXECUTE QUERY */
$results->bind_result($id, $list); /* ASSIGN THE RESULT TO THESE VARIABLES CORRESPONDINGLY */
$results->fetch(); /* FETCH THE RESULT */
$results->close(); /* CLOSE THE PREPARED STATEMENT */