Php Page url ID & Mysql data

Hi,

I’ve been playing around with a page I want to create using php and mysql data.

My goal is to have a page url look like http://somesite.com/?page=pagename.
I also want when the page is loaded that “pagename” relates to a mysql table entry. So that I can then echo the page title and content from my mysql database.

<?php echo "$pagetitle"; echo "pagecontent"; ?>

This is the code that I have tried to piece together using code that I had from a different project:

<?php include('dbconnect.php'); $page = $_GET['pagename']; $dispage = mysql_query("SELECT page FROM pages WHERE page = '$page'"); $default = mysql_query("SELECT page FROM pages WHERE page = home"); if(mysql_num_rows($dispage) !== 0) { include $dispage; } elseif(mysql_num_rows($default)) { include $default; } else { echo 'The page your requested does not exist.'; } ?>

I’m not quite sure how else to explain what I’m trying to do, hopefully someone gets the idea.

-Thanks
Sean

you were pretty close, you needed to fetch the data from the database and echo it, since it is not a file which you can include and you needed to escape the data from the GET variable to help protect your database.
[php]

<?php echo "$pagetitle"; echo "pagecontent"; ?>

This is the code that I have tried to piece together using code that I had from a different project:

<?php include('dbconnect.php'); $page = strip_tags($_GET['pagename']); $page=mysql_real_escape_string($page); $dispage = mysql_query("SELECT page FROM pages WHERE page = '$page'"); $thepage=mysql_fetch_assoc($dispage); $default = mysql_query("SELECT page FROM pages WHERE page = home"); $home=mysql_fetch_assoc($default); if(mysql_num_rows($dispage) !== 0) { echo$thepage['page']; } elseif(mysql_num_rows($default)) { echo$home['home']; } else { echo 'The page your requested does not exist.'; } ?>[/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service