What about accessing a database?
Nice question. I'll try to explain it in here. First you need to know your database configuration such as username, password and your database name. If you have them already, then you can create your table using your Database Administration tools. I prefer using phpMyAdmin, you get the latest version of it at
http://phpmyadmin.net.
Create your table using this query:
CREATE TABLE `friends` (
`friends_id` int(10) unsigned NOT NULL auto_increment,
`friends_name` varchar(255) default NULL,
UNIQUE KEY `friends_id` (`friends_id`)
) TYPE=MyISAM;
INSERT INTO `friends` VALUES (1, 'Paul');
INSERT INTO `friends` VALUES (2, 'Matt');
INSERT INTO `friends` VALUES (3, 'John');
INSERT INTO `friends` VALUES (4, 'Mike');
INSERT INTO `friends` VALUES (5, 'Rendi');
INSERT INTO `friends` VALUES (6, 'Pailul');
Open your index.php file and add these lines:
$db_host = "localhost";
$db_user = "database_user";
$db_pass = "database_pass";
$db_data = "database";
$conn = mysql_connect ($db_host, $db_user, $db_pass) OR DIE ("Can't connect to database");
@mysql_select_db ($db_data, $conn);
$query = "SELECT * FROM friends";
$result = mysql_query ($query, $conn);
if (mysql_num_rows ($result) >= 1)
{
$friends = array();
while ($rows = mysql_fetch_array ($result, MYSQL_ASSOC)) array_push ($friends, $rows);
$smarty->assign ("friends", $friends);
}
Open your index.html page and add these lines:
Friends List:
{section name=i loop=$friends}
{$friends[i].friends_name}<br>
{/section}
Now you'll see the list of friends on your page. You can do some other formatting such as nl2br, truncate, upper, or lower directly from html template page, not from your script. That is why we use Smarty, to separate between business logic and presentation logic. The script should only work to pull data from db but not to format the result. The formatting of the result is done in the html page.
Source:
http://codewalkers.com/tutorials/56/4.html