ArcEmu: Tutorial: Using A Automatic Page Script With A Database. - ArcEmu

Jump to content

Toggle shoutbox Lastest Announcements

dfighter  : (07 December 2014 - 12:06 PM) Arcemu is in hibernation mode, please read http://arcemu.org/fo...showtopic=26903
dfighter  : (01 January 2013 - 05:56 PM) Arcemu wishes you all a happy new year!
Hasbro  : (12 September 2012 - 10:01 AM) Please excuse our outage from the web! Our web host had a major malfunction!
dfighter  : (01 September 2012 - 04:05 PM) Since the spam bots just don't want to stop, I've enabled admin verification when registering.
dfighter  : (23 January 2012 - 09:56 PM) Please note that from now on you will need to confirm your email on the wiki in order to edit it!
Hasbro  : (31 December 2011 - 12:50 PM) Happy New Years all!
Navid  : (26 December 2011 - 04:09 AM) Merry Christmas !!!!!! Happy holidays all :)
WAmadeus  : (24 December 2011 - 03:54 PM) Merry Christmas to all!
dfighter  : (24 December 2011 - 11:05 AM) The Arcemu team wishes y'all a Merry Christmukkah!
Hasbro  : (05 October 2011 - 12:53 PM) Looking for web designers for upcoming web related project. If you're interested in designing user interfaces contact me
dfighter  : (02 September 2011 - 03:47 PM) So who here wants vehicles in Arcemu? :P http://arcemu.org/fo...showtopic=25440
Hasbro  : (14 August 2011 - 03:25 PM) Join us on irc, grab an irc client and connect to irc.freenode.net join channel #arcemu /server irc.freenode.net:6667 /join #arcemu
jackpoz  : (03 August 2011 - 05:33 AM) to all Lua Engine (old one) users: please check http://arcemu.org/fo...showtopic=25274
Hasbro  : (20 May 2011 - 05:27 PM) Looking for people experienced with CMake configuration and setup! Contact me asap
Hasbro  : (15 May 2011 - 05:03 PM) ArcEmu is recruiting C++ programmers, contact Hasbro if interested.
paroxysm  : (03 May 2011 - 06:26 PM) Updated luabridge gossip example to describe the whole gossip creation process rather than just how to create menu. Gossip tutorial
paroxysm  : (23 April 2011 - 11:35 AM) Lua writers can refer to the Luabridge Tutorials section in the Wiki to learn how to write gossip code correctly.
Hasbro  : (20 April 2011 - 05:22 PM) Thank you for your continuous contribution of bug reports, we are working on them.
Hasbro  : (17 April 2011 - 03:20 AM) Please consider donating to support our bills. Donations can be sent using PayPal to donations@arcemu.org - Thank you for your support.
paroxysm  : (10 April 2011 - 12:43 AM) Refer to the Luabridge Tutorials section in the Wiki to learn the new syntax of luabridge.
Resize Shouts Area

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Tutorial: Using A Automatic Page Script With A Database.

#1 User is offline   KHDreams 

  • Member
  • Pip
  • Group: Members
  • Posts: 43
  • Joined: 09-July 08

Posted 24 January 2010 - 09:01 AM

This should be easy. Be sure you've got the knowledge of the following:
How to use HTML
How to use MySQL
How to edit a page.
How to save a page with an extension.
How to upload your page.
How to use your brains (<-- most important)

First, we are going to make the page we are requesting, able to connect to our database.
To make this easy, open a clean document in Notepad or Notepad++. I preffer Notepad++ since it highlights your code.

(You can skip this step if you are a noob who is a compleet morron and wants to do it quick. Or if you are experienced with coding)
If you are using Notepad++ save the file directly (yes, while its empty) and save it as inc_config.php, in your root folder of your main page.
(ex. If you put your website here: c:/mywebsite/ . Then put your document there as well. So it will be c:/mywebsite/inc_config.php)
Now close the program, and reopen it. Why do you ask? Now its automaticly highlighting your code.

Next we are putting in some code.
First we are going to make some variables. Since many websites change from database once in a while, it would be great if we could edit it, without looking through the whole code.
What we need for a database connection is: Databaseuser, Databaseuserpassword, Databasename and Databaselocation.
In my case will that be:
user: root
pass: duhhisasecret
databasename: whatuwant
location: localhost/127.0.0.1

So lets make the variables

inc_config.php
<?PHP
$dbuser="root";
$dbpass="duhhisasecret";
$dbname="whatuwant";
$dbloc="localhost";

?>


So now we've added the variables.
Now let them connect right?

inc_config.php
<?PHP
$dbuser="root";
$dbpass="duhhisasecret";
$dbname="whatuwant";
$dbloc="localhost";

// Let them connect here:
mysql_connect("$dbloc", "$dbuser", "$dbpass")or die('Could not connect: ' . mysql_error());
?>

The "or die" part is to return any errors there may have come.
Lets try it out. Upload your inc_config.php and go to www.yourwebsite.com/inc_config.php
Now this is tricky. U SHOULD see a white page, but it could also mean there is a error in your php. Lets say its not.

So we are moving on. Connection is there. But we still need to select the database. We simply have to add one simple line.

<?PHP
$dbuser="root";
$dbpass="duhhisasecret";
$dbname="whatuwant";
$dbloc="localhost";

// Let them connect here:
mysql_connect("$dbloc", "$dbuser", "$dbpass")or die('Could not connect: ' . mysql_error());

// add the line for the dbselection here:
mysql_select_db("$dbname");
?>


Easy right? Now we are done with creating our configuration and creating a connection.

We want to get a certain page of our database. This HAS to be DONE in our header, where our configuration will be. So let's create a variable where he 'gets' the page out of the URL of the visitor.
using this: $_GET[id];
We add that to our config AS a variable

<?PHP
$dbuser="root";
$dbpass="duhhisasecret";
$dbname="whatuwant";
$dbloc="localhost";

// Let them connect here:
mysql_connect("$dbloc", "$dbuser", "$dbpass")or die('Could not connect: ' . mysql_error());

// add the line for the dbselection here:
mysql_select_db("$dbloc");

// getting the page:
$page=$_GET[id];
?>


We are done with our config. Save it now. And open a new page.

Put in the following:
<html>
<head>
<title> My first automatic page script with a database</title>
</head>
<body>

</body>
</html>


To be official correct we need doctypes, but that doesnt matter for this tutorial.

Now lets include our config. really include. By adding a php tag to our new page. Between the 2 header tags.

<html>
<head>
<?PHP
include('inc_config.php');
?>
<title> My first automatic page script with a database</title>
</head>
<body>

</body>
</html>


Now we are going to add some code. Which isn't that important to explain. It simply look at the url variable. Uses that for a SQL query, and roll out the results. But also checks if its filled yes or no, if it isnt. it will give you the first page. (id 1) Besure that ID 1 will be used.

<html>
<head>
<?PHP
include('inc_config.php');
?>
<title> My first automatic page script with a database</title>
</head>
<body>
<?PHP
if ($page==""){
$getpage="SELECT * FRON pages WHERE id='1'";
$getpage2=mysql_query($getpage);
while (getpage3=mysql_fetch_array($getpage2)){
print "<p><b>$getpage3[title]</b></p>";
print "$getpage3[content]";
}
}
else
{
$getpage="SELECT * FROM pages WHERE id='$page'";
$getpage2=mysql_query($getpage);
while ($getpage3=mysql_fetch_array($getpage2)){
print "<p><b>$getpage3[title]</b></p>";
print "$getpage3[content]";
}
}
?>
</body>
</html>


After this, we close our MySQL connection. with this: mysql_close()

We add this at the bottom of our page.

<html>
<head>
<?PHP
include('inc_config.php');
?>
<title> My first automatic page script with a database</title>
</head>
<body>
<?PHP
if ($page==""){
$getpage="SELECT * FRON pages WHERE id='1'";
$getpage2=mysql_query($getpage);
while (getpage3=mysql_fetch_array($getpage2)){
print "<p><b>$getpage3[title]</b></p>";
print "$getpage3[content]";
}
}
else
{
$getpage="SELECT * FROM pages WHERE id='$page'";
$getpage2=mysql_query($getpage);
while ($getpage3=mysql_fetch_array($getpage2)){
print "<p><b>$getpage3[title]</b></p>";
print "$getpage3[content]";
}
}
?>
</body>
<?PHP
mysql_close()
?>
</html>


And then we are done. Save this new file as page.php in the root directory Now we need to create the database table "pages"
So we will.

If you followed this, you would have knowledge of SQL so I'll only give you hints.

we need 3 pieces
id
title
content

id: int(32), auto_increment
title: varchar(255)
content: text

just create that, and create the first page.
Everytime you create a page, dont fill in the id, if u've done it well, it will automaticly do that for you. So you wont mess up the database.
When filling in the content, use HTML to make your text bold, or hit the next line. etc.
Now.
Lets try it out.
upload the page.php next to the inc_config.php
open your website now:
www.yourwebsitename.com/page.php

It should give you the first page of our database, this is your 'home' page.

now add this to your url:
?id=2

now it will show an other page, with the same file. Handy huh? You can create your menu like this:
home href="?id=1"
contact href="?id=2"

etc

If you've got any question, shoot them up.

This is the way like i would do it if i need a page system really fast.

This post has been edited by KHDreams: 24 January 2010 - 04:23 PM

0

#2 User is offline   Bob Herman 

  • Interested
  • PipPipPipPip
  • Group: Members
  • Posts: 452
  • Joined: 11-October 08
  • Gender:Male

Posted 24 January 2010 - 09:40 AM

Great tut! I wish I had this to use back when I was learning php :)

Just a couple things. It's really useful when you structure your code. Also, the $_GET['id'] doesn't have to be in the header. As long as it's before the main page's script, it'll work. Here's what I would use:

<html>
  <head>
    <title> My first automatic page script with a database</title>
  </head>
  <body>
<?PHP
include('inc_config.php');
if (empty($page)){
  #This now gets the 10 latest news, instead of the very first ones
  $query = mysql_query("SELECT * FROM pages ORDER BY id DESC LIMIT 10");
  while ($getpage = mysql_fetch_array($query)) {
    print "<p><b>$getpage[title]</b></p>";
    print "$getpage[content]";
  }
} else {
  $query = mysql_query("SELECT * FROM pages WHERE id='$page'");
  while ($getpage = mysql_fetch_array($query)){
    print "<p><b>$getpage[title]</b></p>";
    print "$getpage[content]";
  }
}
?>
  </body>
</html>

0

#3 User is offline   KHDreams 

  • Member
  • Pip
  • Group: Members
  • Posts: 43
  • Joined: 09-July 08

Posted 24 January 2010 - 02:31 PM

It's not neccesary, indeed. Though I recommend to use it there. Also, I don't recommend using the mysql connect in the center of your page. Making the connection before building the page, will result a faster database outroll.
0

#4 User is offline   Doggie52 

  • arcemu feedback police
  • PipPipPip
  • Group: Members
  • Posts: 297
  • Joined: 09-October 08
  • Gender:Male
  • Location:Sweden
  • Interests:web designing, web programming, wow databases, game- and webservers
  • Server OS:Windows

Posted 24 January 2010 - 02:38 PM

Come on, people - it's <head>! Not <header> :) !
0

#5 User is offline   KHDreams 

  • Member
  • Pip
  • Group: Members
  • Posts: 43
  • Joined: 09-July 08

Posted 24 January 2010 - 03:06 PM

No it isnt. According to the most doctypes, example strict XHTML, its <header> not <head>
0

#6 User is offline   Bob Herman 

  • Interested
  • PipPipPipPip
  • Group: Members
  • Posts: 452
  • Joined: 11-October 08
  • Gender:Male

Posted 24 January 2010 - 03:11 PM

The most common form used is <head> though :)
0

#7 User is offline   Doggie52 

  • arcemu feedback police
  • PipPipPip
  • Group: Members
  • Posts: 297
  • Joined: 09-October 08
  • Gender:Male
  • Location:Sweden
  • Interests:web designing, web programming, wow databases, game- and webservers
  • Server OS:Windows

Posted 24 January 2010 - 03:41 PM

View PostKHDreams, on 24 January 2010 - 03:06 PM, said:

No it isnt. According to the most doctypes, example strict XHTML, its <header> not <head>

No, you are wrong.

Use the validator, try direct input. None of your code validates.
http://www.webrefere...ence/xhtml.html
http://www.w3schools.com/
http://www.w3.org/TR/xhtml1/

etc... <header> is not and has never been a valid HTML tag, and neither an XHTML tag for that matter.
0

#8 User is offline   KHDreams 

  • Member
  • Pip
  • Group: Members
  • Posts: 43
  • Joined: 09-July 08

Posted 24 January 2010 - 04:22 PM

Wait.. wtf.. Lol, should be a typo? xD Since I'm calling the Head tags the header tags.. Sorry guys. My fault :P. SORRY SORRY SORRY. You guys are right xD. Pff, feeling stupid now. But still, doesnt really matter in the tutorial i guess.. It's about the PHP :)

Sorry for double post, found another typo in the inc_config.php

Going to edit my tutorial right now :P
0

#9 User is offline   thebillkidy 

  • Newbie
  • Group: Members
  • Posts: 0
  • Joined: 16-April 09
  • Gender:Male

Posted 28 May 2010 - 07:35 AM

great only i should change the print to echo and some
other minor fixes
0

#10 User is offline   Bob Herman 

  • Interested
  • PipPipPipPip
  • Group: Members
  • Posts: 452
  • Joined: 11-October 08
  • Gender:Male

Posted 28 May 2010 - 05:49 PM

Print's the same thing as echo, except print is supposedly faster. I don't know why almost everyone uses echo though...
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users