Show page - on empty placeholder

[php]<?PHP //Set $thisPage - ?page=
$thisPage=$_GET[‘page’];
//Start of SQL query
//Including mysql info file
include(‘inc/mysql.php’);
//open connection
$con = mysql_connect($server,$dbusername,$dbpasswrd);
//check if connection is valid, if not - error + error string.
if (!$con){die('Could not connect: ’ . mysql_error());}
//Select database
mysql_select_db($database, $con);
//Define SQL query
$result = mysql_query(“SELECT * FROM site_pages WHERE page = ‘$thisPage’”);
//Execute SQL query, and show content
while($row = mysql_fetch_array($result)){
$page = htmlspecialchars($row[‘page’]);
if($thisPage==$page){$sqlpage=$thisPage;}
//Check for valid page, before running SQL query.
//Start of SQL query
//Including mysql info file
include(‘inc/mysql.php’);
//open connection
$con = mysql_connect($server,$dbusername,$dbpasswrd);
//check if connection is valid, if not - error + error string.
if (!$con){die('Could not connect: ’ . mysql_error());}
//Select database
mysql_select_db($database, $con);
//Define SQL query
$result = mysql_query(“SELECT * FROM site_pages WHERE page = ‘$sqlpage’”);
//Execute SQL query, and show content
while($row = mysql_fetch_array($result)){
$headline = htmlspecialchars($row[‘pagetitle’]);
$date = htmlspecialchars($row[‘date’]);
$author = htmlspecialchars($row[‘author’]);
$content = htmlspecialchars($row[‘content’]);
?>

<?PHP echo $headline; ?>


- <?PHP echo nl2br($date); ?> - <?PHP echo nl2br($author); ?>


<?PHP echo nl2br($content); ?>


<?PHP }
//close connection
mysql_close($con);
?>[/php]

Hi, I’ve written this file - all it does is check the DB for the pages, if the page exist in the table - tjen it will read the row and show the contents.
I’m trying to figure how I can get it to show a page when “?page=” is set to nothing in the address bar? …
Like showing the first entry from the db?

Any ideas?

First, you’re repeating the same code twice (connecting to db, querying page) - this is not needed. Even if you need to execute another query, there is no need to create new connection to MySQL within the same script.
As for your question, you can do the following:

[php]
if($thisPage!=’’){
$result = mysql_query(“SELECT * FROM site_pages WHERE page = '”.mysql_real_escape_string($thisPage)."’");
if(mysql_num_rows($result)){
// page found
}
else{
// page not found - reset to empty
$thisPage=’’;
}
}

if($thisPage==’’){ // page is not defined - select first from db
$result = mysql_query(“SELECT * FROM site_pages ORDER by id LIMIT 1”);
}
[/php]

Thanks
Where exactly do you want me to put this code? - I dont see any html that outputs the content of the row?

Also I’m getting this error :S

[08-Aug-2010 12:51:14] PHP Parse error: syntax error, unexpected T_ELSE in /Applications/MAMP/htdocs/dev/inc/content.php on line 8
[php] include('inc/mysql.php'); if($thisPage!=''){ $result = mysql_query("SELECT * FROM site_pages WHERE page = '".mysql_real_escape_string($thisPage)."'"); if(mysql_num_rows($result)){ { // page found } else{ // page not found - reset to empty $thisPage=''; } } if($thisPage==''){ // page is not defined - select first from db $result = mysql_query("SELECT * FROM site_pages ORDER by id LIMIT 1"); } mysql_close($con); } [/php]

The code what I posted is a replacement of this line in your code (first instance of this line):
[php]
$result = mysql_query(“SELECT * FROM site_pages WHERE page = ‘$thisPage’”);
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service