Jump to content

[SOLVED] If column exists, if not..


peter_anderson

Recommended Posts

Hi all,

 

I'm trying to create a small CMS, which gets the page from the variable at the URL (eg page.php?page=XXXX), but now I need to redirect it to 404 if the page does not exist.

 

Here's some of my code (the rest deals with the theme, user login, etc)

//get page
$pg = $_GET['page'];

//connect to DB
mysql_connect($host, $user, $pass) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());

	//query
	$query = mysql_query("SELECT * from `page` WHERE id='$pg'") or die(mysql_error());  
	// Perform Query
	$row = mysql_fetch_array($query);

 

I'm thinking I should use an If statement, but what should the condition be?

 

if ( $number_three == 3 ) {
//adds content to theme
} else {
//php headers redirect to 404 page
}

 

Any help?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/170413-solved-if-column-exists-if-not/
Share on other sites

$query = mysql_query("SELECT * from `page` WHERE id='$pg'") or die(mysql_error());
if(mysql_num_rows($query)) ==0) 
{
header("Location: your404page.php"); 
exit; 
}
$row=mysql_fetch_array($query); 
// The rest of your code here 

 

Thanks :)

 

You had an extra bracket, but it works :D

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.