![Likes](https://www.mmopro.org/dbtech/thanks/images/likes.png)
Results 1 to 7 of 7
-
25-07-08, 02:55 AM #1
Simple website with PHP and MySQL
Register to remove this adHello.
I think I'll be the first to post here, with a simple tutorial to make a PHP-MySQL-based website.
So here's our config file, called "config.php":
PHP Code:<?php
$sql_host = "localhost";
$sql_user = "root";
$sql_pass = "P@ssw0rd";
$sql_db = "test";
?>
PHP Code:<?php
include_once("config.php");
?>
The next thing we're going to do, is to connect to MySQL. I know there's no tables to use yet, but that will come. ^^
Your code should look like this now:
PHP Code:<?php
include_once("config.php");
mysql_connect($sql_host, $sql_user, $sql_pass) or die("Could not connect to MySQL!");
mysql_select_db($sql_db) or die("Could not select database!");
?>
Now we'll create our tables, and add some example data.
Execute this code to MySQL:
Code:CREATE TABLE `content` ( `id` int(10) NOT NULL AUTO_INCREMENT, `title` varchar(255) NOT NULL, `text` text NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `id` (`id`) ); INSERT INTO content VALUES (1, 'Test 1', 'This is some example text of the first test-page.'); INSERT INTO content VALUES (2, 'Test 2', 'This is some example text of the second test-page.');
PHP Code:<?php
// Get our config vars.
include_once("config.php");
// Connect to MySQL and select the database to use.
mysql_connect($sql_host, $sql_user, $sql_pass) or die("Could not connect to MySQL!");
mysql_select_db($sql_db) or die("Could not select database!");
// Make sure the id we get is valid, so we don't get some invalid queries
if(isset($_GET['id']) && !empty($_GET['id']) && $_GET['id'] > 0) {
$id = intval( (int) $_GET['id'] );
} else {
// Else just use id 1
$id = 1;
}
// Select the column, and escape the id for more security
$sql = "SELECT * FROM content WHERE id = '" . mysql_escape_string($id) . "'";
// Query the database
$q = mysql_query($sql);
?>
PHP Code:<?php
// Get our config vars.
include_once("config.php");
// Connect to MySQL and select the database to use.
mysql_connect($sql_host, $sql_user, $sql_pass) or die("Could not connect to MySQL!");
mysql_select_db($sql_db) or die("Could not select database!");
// Make sure the id we get is valid, so we don't get some invalid queries
if(isset($_GET['id']) && !empty($_GET['id']) && $_GET['id'] > 0) {
$id = intval( (int) $_GET['id'] );
} else {
// Else just use id 1
$id = 1;
}
// Select the column, and escape the id for more security
$sql = "SELECT title, text FROM content WHERE id = '" . mysql_escape_string($id) . "'";
// Query the database
$q = mysql_query($sql);
// Check for a valid page id
if(mysql_num_rows($q) != 1) {
$title = "404 - Not found";
$text = "The requested page could not be found.";
} else {
// Store the data into $title and $text
list($title, $text) = mysql_fetch_array($q);
}
// Free memory from the query
mysql_free_result($q);
// Close connection to MySQL
mysql_close();
?>
Now we just need to output it.
Here's the data inside some basic HTML:
PHP Code:<html>
<head>
<title>My First PHP-MySQL-Website</title>
</head>
<body>
<table width="100%" height="100%">
<tr>
<td align="center" valign="middle">
<table width="600">
<tr>
<td><h1><?php echo $title; ?></h1></td>
</tr>
<tr>
<td><?php echo $text; ?></td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
PHP Code:<?php
// Get our config vars.
include_once("config.php");
// Connect to MySQL and select the database to use.
mysql_connect($sql_host, $sql_user, $sql_pass) or die("Could not connect to MySQL!");
mysql_select_db($sql_db) or die("Could not select database!");
// Make sure the id we get is valid, so we don't get some invalid queries
if(isset($_GET['id']) && !empty($_GET['id']) && $_GET['id'] > 0) {
$id = intval( (int) $_GET['id'] );
} else {
// Else just use id 1
$id = 1;
}
// Select the column, and escape the id for more security
$sql = "SELECT title, text FROM content WHERE id = '" . mysql_escape_string($id) . "'";
// Query the database
$q = mysql_query($sql);
// Check for a valid page id
if(mysql_num_rows($q) != 1) {
$title = "404 - Not found";
$text = "The requested page could not be found.";
} else {
// Store the data into $title and $text
list($title, $text) = mysql_fetch_array($q);
}
// Free memory from the query
mysql_free_result($q);
// Close connection to MySQL
mysql_close();
?>
<html>
<head>
<title>My First PHP-MySQL-Website</title>
</head>
<body>
<table width="100%" height="100%">
<tr>
<td align="center" valign="middle">
<table width="600">
<tr>
<td><h1><?php echo $title; ?></h1></td>
</tr>
<tr>
<td><?php echo $text; ?></td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
You add more pages through MySQL, as I did earlier in this tutorial.
You change page through the URL's query-string, e.g "index.php?id=2", "index.php?id=7", "index.php?id=727", and so on.
Please leave comments/suggestions, then I might add more to this soon.
-ChimP
› See More: Simple website with PHP and MySQLLast edited by ChimP; 25-07-08 at 02:59 AM.
-
25-07-08, 07:12 AM #2
Tables? Lol chimp... but yea "simple"
Nice workfrom meh man!
Btw I am into creating Web 2.0 Designs now validated,...
-
25-07-08, 09:15 AM #3
- Rep Power
- 17
- Reputation
- 11
How is this supposed to help people... People who dont know will say "WTF is this"
-
27-07-08, 01:20 PM #4
Originally Posted by LuMieRe
Just did it this time, so I don't have to learn people a lot of CSS too. ^^
Originally Posted by ghost
And as the title says, it's supposed to help people to start making a PHP-MySQL-based website.
-
02-08-08, 07:12 PM #5
-
17-08-08, 09:28 PM #6
- Rep Power
- 17
- Reputation
- 11
Lol, ChimP is the Professional
Havent seen any 1 else better than him in pHP :S
-
27-08-08, 12:39 PM #7
Register to remove this adShould I give a release of a template-based controller system?
I simply made this tutorial to help those who don't know PHP very well, so they got a chance to get started with the basics about PHP+MySQL.
Currently I'm too busy to make more tutorials.
But when I got time for it, I might release my controller system with a guide how to use it and some examples.
But that's for the pretty experienced users, since it's using multiple classes and templates. ^^Last edited by ChimP; 27-08-08 at 12:47 PM.