How to make sure a database entry exists?

I’m working on a way to get the page title for any particular page and part of that involves using $_GET to see if ?p, ?page or ?category is set.

What I am interested in knowing is, is there a way to ensure that the information is passed as a digit and how to prevent an error if that entry doesn’t exist in the database?

So for instance a good number would be

index.php?p=1

What I want to prevent/not have crash the page is:
index.php?p=foo
and
index.php?p=999(a page that doesn’t exist)

Thanks for reading.

submitting a query to the database engine, and getting a response back that says there is no such row… this is ~not~ an error condition, and there is no way it should “crash” the page

:slight_smile:

If you want to make sure a $_GET parameter consists of digits only, you can use [fphp]ctype_digit[/fphp].

So it returns false? So you could do an “If false do this” thing to deal with it? Wonder what I’m doing wrong then, if I try to pass a number that doesn’t exist it complaints about my syntax.

Is there an advantage to ctype over is_numeric? I’ve never heard of ctype before.

Yes – check them out in the manual: [fphp]is_numeric[/fphp], [fphp]ctype_digit[/fphp]

Ah I see, numeric operators count in is_numeric.

Thanks

?p=foo and ?p=999 are different situations.

The first, you’ll quit with an error before even attempting a query, because someone has fiddled with the value inappropriately.

For 999, it’s a perfectly valid query, so you can use num_rows or similar to see if no result is matched.

For bonus points you’ll probably want to issue a 404 header if the record (page) isn’t found so these pages don’t get indexed if you remove records.

So I’d check if Numrows != 0? Such a simple thing and I never thought of that. Thanks/