du9826 2011-09-04 22:24
浏览 23
已采纳

在某个php字典上创建错误页面

I am trying to create an error page but I want to specify it on a certain dictionary. the problem is that it is a php file. for example if I have a page with a title called page1 only and do not have anything else, I want to display an error for every other page that does not exist like page2. http://example.com/showpage.php?title=page1 http://example.com/showpage.php?title=page2 I do not want to use htaccess because there is other files in the same dictionary that I want to display another error message.

  • 写回答

2条回答 默认 最新

  • doushou6480 2011-09-04 23:19
    关注

    Do you query against a database or do you include real files like page1.php for example?

    EDIT: If you are using MySQL, then you want to use mysql_num_rows() against your query and if that returns true, do all the echoing, but if it returns false, then include_once('error-for-non-existing-title.php'); Optionally if no $title variable is set, you could throw a 404 header. You can rewrite your 404 documents in .htaccess if you want to

    I think for an online-reader, you should have more than one variable ($_GET['title']) AND $_GET['page']. Use $title to differentiate between different titles and $page to navigate through it's pages.

    I guess you don't know how to use PDO yet, so..

    $link = mysql_connect("localhost", "username", "password");
    mysql_select_db("database_name", $link);
    
    if(isset($_GET['title']))  {
    $title = $_GET['title'];
    
    $query = mysql_query("SELECT * FROM pages WHERE title='$title'", $link);
    
    $num_rows = mysql_num_rows($query);
    
    //if there is a page
    if($num_rows!=0) {
    
    //fetch the data as associative array
    $result = mysql_fetch_assoc($query);
    
    echo $result['title'],'<br />';
    }
    else include_once('error.php');
    }
    
    //optionally if you want to, you could throw a 404 header if there is no $title
    //variable set. Just remove the double slashes from the next line of code.
    //else header("HTTP/1.1 404 Not Found");  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)