我试图避免两次查询我的数据库:设置<title>
属性和回显页面标题。我只想查询一次:
例子:
<html>
<head>
<?php
// how should I use here ob_start() ? Is there any other possible way to achieve this?
$title = "";
echo '<title>', $title, '</title>'; // this should be in the end <title>value of $row['page_title']</title>
?>
</head>
<body>
<?php
$sql = $mysqli->query("MY QUERY");
$row = $sql->fetch_assoc();
$title = $row['page_title']; // I want that this assignment to set the variable in the top
// I know that for this job I can use ob_start() but I didn't used it until now
// and I will really appreciate any help from you.
?>
<h1><?php echo $title; ?></h1>
</body>
</html>
我知道我可以在回显title
属性之前进行查询,但我不想那样做。你有什么建议吗?或者你能告诉我如何使用那个ob_start()
/ob_clean()
功能吗?
谢谢!