Forum Topic: Php: Page Id's (using Mysql)

(1,772 views • 6 replies)

This topic is 1 page long.

<< < > >>
None

Jordan

Reply To Post Reply & Quote

Posted at: 11/19/06 05:19 AM

Jordan DARK LEVEL 14

Sign-Up: 04/23/06

Posts: 2,833

Php: Main

What is this tutorial about?
I am going to show you how to make a page which pulls data from a database depending on the page ID.

What are the requirements?
A host with Php and Mysql. Although, you can Setup localhost and run the whole thing from your computer. Also a basic php and maybe some sql knowledge would be nice.

Is this secure?
Yes, it should be secure enough. I will be using security techniques like addslashes and mysql_real_escape_string.

Okay, so lets get started!

First of all, make a php file called index.php, or pages.php. Although i will refer to it as index.php throughout this tutorial.

At the very top of this php page, add this code:

<?php
$page = $_GET['page'];
addslashes($page);

$conn = mysql_connect("localhost","Username","Passwor d");
if (!$conn)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("pages", $conn);
?>

I shall now explain this code below.

<?php
$page = $_GET['page'];
addslashes($page);

This gets the page value from the url, for example if you went to index.php?page=LOL it would return LOL into that $page variable. We now apply addslashes() to the $page variable for security reasons, as we are going to be using this in a MySQL query.

$conn = mysql_connect("localhost","Username","Passwor d");
if (!$conn)
{
die('Could not connect: ' . mysql_error());
}

This will connect to the mysql database, and produce a mysql error if it can't connect for any reason. You must fill out your username and password; if your mysql server isn't localhost, you'll also have to change that.

mysql_select_db("pages", $conn);
?>

This code will select the database "pages", which you must either create. Or enter the name of an already made database into there.

Right, that is the connect + select DB script done. Now to make the script that will pull the data down from the database.

But first, we will need to make the table. To do this, execute this sql query in your PhpMyAdmin:
CREATE TABLE pages
(
pageid varchar(30),
content text
)

I'll just briefly explain the above code.
It creates a table in the database called pages. And in this table, it creates two collums: pageid and content. As you can see, pageid is a varchar(30). That means it can be a string of any letters/numbers upto 30 characters. And the text datatype on content means that it is just really long, raw text.

Okay, so we have our table and connect script, just the select script now:
<?php
mysql_real_escape_string($page,$conn);
$sql = "SELECT * FROM pages WHERE pagename = '$page' LIMIT 1";
$result = mysql_query($sql);
$result2 = mysql_fetch_assoc($result);
$content = $result2[content];
if(!$content==""){
echo $content;
}
else{
echo "Either this page doesn't exist, or there was some other error.";
}
?>

I'll now explain the above code.

mysql_real_escape_string($page,$conn);
This is another security feature, we are applying it on the $page variable.
Better explained here.

$sql = "SELECT * FROM pages WHERE pageid = '$page' LIMIT 1";
$result = mysql_query($sql);
$result2 = mysql_fetch_assoc($result);

This code defines the sql query we are going to be using on the database, then executes it in mysql_query as the variable $result. It now applies mysql_fetch_assoc to that result, which returns an associative array of the data we fetched from the database.

$content = $result2[content];
This sets the variable $content to the content part of the row where the pageid is the one that is stored in the $page variable.

if(!$content==""){
echo $content;
}
else{
echo "Either this page doesn't exist, or there was some other error.";
}

This checks if the $content variable contains any data, if it does, it will echo it out. If it doesn't, it will show our error.

Now i'm going to explain the above sql query:
SELECT * FROM pages
This selects all data from the table pages, * is a wildcard.

WHERE pageid = '$page'
This makes it only return data when the pageid is the same as the $page variable, from earlier on in this tutorial.

LIMIT 1
This will make it only return the first row it finds.

-Continued on post 2-


None

Jordan

Reply To Post Reply & Quote

Posted at: 11/19/06 05:20 AM

Jordan DARK LEVEL 14

Sign-Up: 04/23/06

Posts: 2,833

We are nearly done, we just need to add some pages!
I am not going to show you how to make a form and php page which will add pages to the database in this tutorial. As you can easily add this in PhpMyAdmin. I've added a picture onto this tutorial to explain how to add the pages in PhpMyAdmin. First off, navigate to your database which has our pages table in. Now select the browse button on your pages table(See picture1). Now that you have done that, you must select the insert button at the top(See picture2). Once you are on the insert page, it is self explanatory what you have to do. Write out the page's content and the pageid, note that you can use html/javascript in your content collum. But you can't do php.

Okay, so you have added a page. Now, go to index.php?page=Your Pageid and you should see your page you just entered into PhpMyAdmin, try anything else and you just get the error we defined earlier.

You might want to add a design to this, by adding your header above the second script, and your footer below it. Although the point of this tutorial isn't to teach you how to make a design.

I'm just going to explain 1 more thing, which i prefer to use when doing something like this as it can be indexed by search engines is htaccess.

I'm not going to explain how to use htaccess, i'm just going to give the code that i used:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^page/(.*).html index.php?page=$1

Then if you have that htaccess in the main directory, going to domain.com/page/lol.html is actually sending back domain.com/index.php?page=lol.

So, that concludes this tutorial.

-Jordan


None

Jordan

Reply To Post Reply & Quote

Posted at: 11/19/06 05:23 AM

Jordan DARK LEVEL 14

Sign-Up: 04/23/06

Posts: 2,833

Ooops, forgot the image:

Php: Page Id's (using Mysql)


None

Zendra

Reply To Post Reply & Quote

Posted at: 11/19/06 06:49 AM

Zendra NEUTRAL LEVEL 41

Sign-Up: 09/07/03

Posts: 12,465

Why don't make a is_numeric check to see if the value is a number - no need to add those slashes.

NG BBS & Review moderator // PM abuse // Check my audio!


None

elbekko

Reply To Post Reply & Quote

Posted at: 11/19/06 07:48 AM

elbekko EVIL LEVEL 16

Sign-Up: 07/23/04

Posts: 6,587

At 11/19/06 06:49 AM, Zendra wrote: Why don't make a is_numeric check to see if the value is a number - no need to add those slashes.

Or just use intval()

"My software never has bugs. It just develops random features. " - Unknown

[ FluxBB developer | Quickmarks 0.5.1 | Strings & Ints - my blog ]

BBS Signature

None

Jordan

Reply To Post Reply & Quote

Posted at: 12/4/06 03:07 AM

Jordan DARK LEVEL 14

Sign-Up: 04/23/06

Posts: 2,833

At 11/19/06 07:48 AM, elbekko wrote:
At 11/19/06 06:49 AM, Zendra wrote: Why don't make a is_numeric check to see if the value is a number - no need to add those slashes.
Or just use intval()

Nope, because i used pagenames with it, not numbers.


Happy

Acid

Reply To Post Reply & Quote

Posted at: 2/12/07 11:22 PM

Acid FAB LEVEL 27

Sign-Up: 06/03/04

Posts: 7,842

great tutorial.


All times are Eastern Standard Time (GMT -5) | Current Time: 06:33 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!