404 if record not found - 如何检查记录是否存在?

发布于 2024-11-28 12:01:50 字数 432 浏览 2 评论 0原文

我认为我在上一个问题中没有充分解释自己。

我有一个页面显示各种元素,即使它从数据库调用的 id 不存在或已被删除(这会引发各种丑陋的错误,并且搜索引擎继续列出不存在的页面)。

如果 $id 不存在,您是否可以修改下面显示的页面代码的第一部分以发送 404(或至少发送到具有 404 标头的 projecterror.php)?非常感谢!

<?php
include_once("includes/linkmysql.php");
$adda=$_GET['a']; 
$cont=$_GET['c']; 
$select="SELECT * FROM projects where id='$id'";
$qselect = mysql_query($select);
while ($row = mysql_fetch_array($qselect)) { 

I don't think I explained myself adequately in my previous question.

I have a page that displays various elements even if the id it's calling from the database does not exist or was deleted (which throws up all sorts of ugly errors along with search engines continuing to list non-existent pages).

Can you modify the first part of the page code shown below to send a 404 (or at least to projecterror.php which has 404 headers) if $id does not exist? Many thanks!

<?php
include_once("includes/linkmysql.php");
$adda=$_GET['a']; 
$cont=$_GET['c']; 
$select="SELECT * FROM projects where id='$id'";
$qselect = mysql_query($select);
while ($row = mysql_fetch_array($qselect)) { 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤独难免 2024-12-05 12:01:51

由于仅依赖 HTTP 错误代码有时可能有点过于模糊,因此我建议您除了常规 HTTP 状态代码之外,还将该错误包含在 XML/JSON 响应消息中。
这样您就可以提供更好的日志记录,并且由于错误在消息中详细说明,因此它将大大缩短调试时间/为您提供更清楚地呈现错误来源的机会。

Since only relying on HTTP error codes sometimes can be a little too ambiguous, I suggest you include the error in an XML/JSON response message in addition to the general HTTP status code.
That way you can provide better logging and since the error is stated in detail in the message it will drastically shorten debugging time / provide the opportunity for you to present the source of the error more clearly.

多情出卖 2024-12-05 12:01:51
<?php
include_once("includes/linkmysql.php");
$adda=$_GET['a']; 
$cont=$_GET['c']; 
$select="SELECT * FROM projects where id='$id'";
$qselect = mysql_query($select);
if( mysql_num_rows( $qselect ) === 0 )
{
   header("HTTP/1.1 301 Moved Permanently");
   header( 'Location: http://examplesite.domain/errorpage' ) ;
   exit;
}
while ($row = mysql_fetch_array($qselect)) { 

这些是@Vivek Goel 在您提出的其他问题上为您提供的标题代码

<?php
include_once("includes/linkmysql.php");
$adda=$_GET['a']; 
$cont=$_GET['c']; 
$select="SELECT * FROM projects where id='$id'";
$qselect = mysql_query($select);
if( mysql_num_rows( $qselect ) === 0 )
{
   header("HTTP/1.1 301 Moved Permanently");
   header( 'Location: http://examplesite.domain/errorpage' ) ;
   exit;
}
while ($row = mysql_fetch_array($qselect)) { 

These are the header codes @Vivek Goel gave you on the other questions you asked

維他命╮ 2024-12-05 12:01:51
include_once("includes/linkmysql.php");
$adda=$_GET['a']; 
$cont=$_GET['c']; 
$select="SELECT * FROM projects where id='$id'";
$qselect = mysql_query($select);
if(!mysql_num_rows($qselect)){
    header($_SERVER['HTTP_PROTOCOL']. '404 Entry Not Found');
    //print other info
}
else{
 //do usual code

}

include_once("includes/linkmysql.php");
$adda=$_GET['a']; 
$cont=$_GET['c']; 
$select="SELECT * FROM projects where id='$id'";
$qselect = mysql_query($select);
if(!mysql_num_rows($qselect)){
    header($_SERVER['HTTP_PROTOCOL']. '404 Entry Not Found');
    //print other info
}
else{
 //do usual code

}

娇俏 2024-12-05 12:01:51

上面的答案中没有人在 MySQLI 中回答它,如果在数据库中找不到记录,则 Mysqli 使用以下

<?php
ob_start();
include_once("includes/config.php")//mysqli config;
$adda=$_GET['a']; 
$cont=$_GET['c']; 
$select="SELECT * FROM projects where id='$id'";
$result = $conn->query($select) or die("Cannot write");
$numrows = $result->num_rows;
if( $numrows === 0 )
{
   ob_end_clean();
   header("HTTP/1.1 301 Moved Permanently");
   header( 'Location: http://examplesite.domain/errorpage' ) ;
   exit();
}
while ($row = mysql_fetch_array($qselect)) {

代码
www.psychocodes.in

No one in the above answers have answered it in MySQLI for Mysqli use the below code if a record is not found in the database

<?php
ob_start();
include_once("includes/config.php")//mysqli config;
$adda=$_GET['a']; 
$cont=$_GET['c']; 
$select="SELECT * FROM projects where id='$id'";
$result = $conn->query($select) or die("Cannot write");
$numrows = $result->num_rows;
if( $numrows === 0 )
{
   ob_end_clean();
   header("HTTP/1.1 301 Moved Permanently");
   header( 'Location: http://examplesite.domain/errorpage' ) ;
   exit();
}
while ($row = mysql_fetch_array($qselect)) {

regards
www.psychocodes.in

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文