如何检查数据库中是否找不到页面信息?如果我使用foreach
检查页面是否存在,我将返回false,如下例所示:
我的路由器文件:
$db = new PDO('mysql:host=127.0.0.1;dbname=project1', 'root', '');
$GetDirectory = $db->prepare('
SELECT *
FROM pages
');
$GetDirectory->execute();
$GetDirectory = $GetDirectory->fetchAll(PDO::FETCH_ASSOC);
if (Route::Request_URL()=='/') {
Route::Navigate_URL('home');
} else {
foreach ($GetDirectory as $Directory) {
switch (Route::Request_URL()) {
case strpos(Route::Request_URL(), $Directory['request']) :
if (Route::Verify_URL($Directory['pos1'], $Directory['pos2'], $Directory['request'])!==false) {
Route::Navigate_URL($Directory['name']);
$db = null;
} else {
//this will give an error for each time it didn't found
}
break;
}
}
}
我的班级档案:
class Route {
public static function Request_URL() {
return $_SERVER['REQUEST_URI'];
}
public static function Verify_URL($pos1, $pos2, $url) {
if (strpos(substr(Route::Request_URL(), $pos1, $pos2), $url)!==false) {
return true;
} else {
return false;
}
}
public static function Navigate_URL($RequestURL) {
$db = new PDO('mysql:host=127.0.0.1;dbname=project1', 'root', '');
$ListDirectory = $db->prepare('
SELECT *
FROM pages
');
$ListDirectory->execute();
$ListDirectory = $ListDirectory->fetchAll(PDO::FETCH_ASSOC);
foreach ($ListDirectory as $Directory) {
switch ($RequestURL) {
case $Directory['name'] :
return require_once SERVE_PATH.$Directory['path'];
break;
}
}
}
}
很抱歉长代码。想问一个更简单的问题,但不知道如何解释。