Jump to content

Checking if a mySQL row exists or not?


uramagget

Recommended Posts

I'm making a Content Management System, so I want the system to check if a page (?id=pagename here) exists or not. If it doesn't, return a 404 error. If not, serve the page. Also, I would like some help in stopping people from entering blank URLs (?id=_blank_stuff_here).

 

Here is what I'm using for the querystring:

 

 

<?
/*
Script Name: query.inc.php
Date: June 03, 2007
Description: ?id= URLs. Eww.....
Version: 0.1
Change Log:
Version 0.1 (Original Version):
*/


//Use GET to find query
$id=mysql_real_escape_string($_GET['id']);
//Use mySQL to select the query
$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>

Link to comment
https://forums.phpfreaks.com/topic/54153-checking-if-a-mysql-row-exists-or-not/
Share on other sites

<?
/*
Script Name: query.inc.php
Date: June 03, 2007
Description: ?id= URLs. Eww.....
Version: 0.1
Change Log:
Version 0.1 (Original Version):
*/


//Use GET to find query
if (isset($_GET['id']) && !empty(trim($_GET['id'])) {
    $id=mysql_real_escape_string($_GET['id']);
    //Use mySQL to select the query
    $sql="SELECT * FROM $tbl_name WHERE id='$id'";
    $result=mysql_query($sql);
    if (mysql_num_rows($result) > 0)
         $rows=mysql_fetch_array($result);
    else
         show_404();
}else {
     show_404();
}

function show_404() {
     header("404 PAGE NOT FOUND");
     include('404.html');
     die();
}
?>

 

Something like that?

[code]

<?php

/*

Script Name: query.inc.php

Date: June 03, 2007

Description: ?id= URLs. Eww.....

Version: 0.1

Change Log:

Version 0.1 (Original Version):

*/

 

 

//Use GET to find query

$_GET['id'] = (isset($_GET['id'']))?trim($_GET['id']):'';

if (!empty($_GET['id'])) {

    $id=mysql_real_escape_string($_GET['id']);

    //Use mySQL to select the query

    $sql="SELECT * FROM $tbl_name WHERE id='$id'";

    $result=mysql_query($sql);

    if (mysql_num_rows($result) > 0)

        $rows=mysql_fetch_array($result);

    else

        show_404();

}else {

    show_404();

}

 

function show_404() {

    header("404 PAGE NOT FOUND");

    include('404.html');

    die();

}

?>

[/code]

 

I know it is the first if statement and is probably due to the empty www.php.net/empty  try the above if the same error report it back.

<?php
/*
Script Name: query.inc.php
Date: June 03, 2007
Description: ?id= URLs. Eww.....
Version: 0.1
Change Log:
Version 0.1 (Original Version):
*/


//Use GET to find query
$_GET['id'] = (isset($_GET['id']))?trim($_GET['id']):'';

if (!empty($_GET['id'])) {
    $id=mysql_real_escape_string($_GET['id']);
    //Use mySQL to select the query
    $sql="SELECT * FROM $tbl_name WHERE id='$id'";
    $result=mysql_query($sql);
    if (mysql_num_rows($result) > 0)
         $rows=mysql_fetch_array($result);
    else
         show_404();
}else {
     show_404();
}

function show_404() {
     header("404 PAGE NOT FOUND");
     include('404.html');
     die();
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.