好的,所以我希望此代码片段检查数据库中是否存在变量 $_GET['p'],如果存在则 make $p = $_GET['p'],如果不存在则 make $p = '1',如果 $_GET['p'] 甚至没有在链接中设置,那么只显示 id 为 0 的页面($p = '0';)。这是我的代码。它仅在链接中设置变量时显示“未知页面”。
if (isset($_GET['p']))
{
$getpage = $_GET['p'];
$sql = sprintf("SELECT * FROM pages WHERE id=%d LIMIT 1", $getpage);
$result = $con->query($sql);
if ($result && $result->mum_rows > 0 ){
// if page id exists in "pages" table, then make $p = $_GET['p'].
$p = $getpage;
}
else
{
// if "p" ID doesn't exist in DB, then show "unknown page" page with id of "1".
$p = '1';
}
}
else if (!isset ($_GET['p']))
{
//if variable "p" isn't set in link then display homepage or page with ID of 0.
$p = '0';
}