Learning PHP and MySQL
Wednesday, September 27, 2006
Example 16-10 The posts.php script displays a listing of posts and their subjects
<?php
session_start();
require_once('config.php');
require_once('db_login.php');
require_once("DB.php");
// Display the page header
$smarty->assign('blog_title',$blog_title);
$smarty->display('header.tpl');
// Check for valid login
if (!isset($_SESSION['username'])) {
echo 'Please <a href="login.php">login</a>.';
exit;
}
// Connect to the database
$connection = DB::connect("mysql://$db_username:$db_password@$db_host/$db_database");
if (DB::isError($connection)){
die ("Could not connect to the database: <br />". DB::errorMessage($connection));
}
// Query the posts with catagories and user information
$query = "SELECT * FROM `users` NATURAL JOIN `posts` NATURAL JOIN `categories`
ORDER BY `posted` DESC";
// Execute the database query
$result = $connection->query($query);
if (DB::isError($result)){
die("Could not query the database: <br />".$query." ".DB::errorMessage($result));
}
// Place the query results into an array
while ($result_row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
$test[] = $result_row;
}
// Send the data to the template
$smarty->assign('posts', $test);
// Display the template with the data plugged in
$smarty->display('posts.tpl');
// Close the database connection
$connection->disconnect();
// Display the page footer
$smarty->display('footer.tpl');
?>
Posted by krautgrrl on 09/27 at 11:43 AM
Chapter 16 Code • (27) Comments • (94) Trackbacks • Permalink
Chapter 16 Code • (27) Comments • (94) Trackbacks • Permalink
Statistics
This page has been viewed 374279 times
Page rendered in 0.1246 seconds
Total Entries: 224
Total Comments: 16
Total Trackbacks: 307338
Most Recent Entry: 09/27/2006 12:39 pm
Most Recent Comment on: 10/26/2007 10:00 am
Total Members: 2
Total Logged in members: 0
Total guests: 9
Total anonymous users: 0
Most Recent Visitor on: 02/06/2012 08:43 am
The most visitors ever was 1103 on 11/20/2007 12:50 pm
