我有数据库名称"recipe"和表名称"contents"。在"contents"中有两列,分别命名为"id"answers"list"。我想在html中将"列表"显示为标题。表"内容"示例
**id列表**1 Malai Paneer2 Kadhai Paneer
所以,我想在html中显示"Malai Paneer"answers"Kadhai Paneer"作为标题。我使用的是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(作为主页面标题)************************
使用此
<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>
很难理解需求到底是什么。然而,这里有一些答案:
- 将所有数据显示为逗号分隔的标题
例如:
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>
我认为使用分配从数据库中提取的数据
$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 */
然后,我们可以通过以下方式回应结果:
echo '<h1>'.$list.'</h1>';