我试图从数据库中获取一些信息并将其用作meta desc和keyword。但事情并没有像预期的那样发挥作用。
编辑:经过很多帮助,我得到了半工作。如果没有blogID,我希望它在另一部分上回落......脚本的那部分工作,任何想法?<?php
$dsn = "sqlsrv:Server=localhost;Database=blog";
$conn = new PDO($dsn, "**********", "********");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$id = $_GET['postID'];
$sql = "SELECT * FROM blog_posts WHERE blogID=:id ORDER BY blogID DESC";
$stmt = $conn->prepare($sql);
$stmt->execute (array($id));
while($metta = $stmt->fetch(PDO::FETCH_BOTH) )
if (isset($metta['blogID']) && !empty($metta['blogID'])) {
$keywords = $metta['keywords'];
$description = $metta['description'];
} else {
$keywords = "blalbalbalblabla";
$description = "blabla";
}
?>
答案 0 :(得分:0)
它可能不是最漂亮的代码......但它有效!
<?php
$dsn = "sqlsrv:Server=localhost;Database=blog";
$conn = new PDO($dsn, "*****", "*******");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$id = $_GET['postID'];
$sql = "SELECT * FROM blog_posts WHERE blogID=:id";
$stmt = $conn->prepare($sql);
$stmt->execute (array($id));
$metta = $stmt->fetch(PDO::FETCH_BOTH);
if (isset($_GET['postID'])) {
$keywords = $metta['keywords'];
$description = $metta['description'];
}
sqlsrv_close($con);
?>
<meta name="description" content="<?= isset($description) ? $description : 'blablabla'; ?>">
<meta name="keywords" content="<?= isset($keywords) ? $keywords : 'blablabla'; ?>">