Learning PHP and MySQL
Wednesday, September 27, 2006
Example 16-12 The view_post.php script displays and a summary of its comments
<?php
session_start();
require_once('config.php');
require_once('db_login.php');
require_once("DB.php");
// Display the 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));
}
$post_id = $_GET["post_id"];
$query = "SELECT * FROM `users` NATURAL JOIN `posts` NATURAL JOIN `categories`
WHERE `post_id`=$post_id";
$result = $connection->query($query);
if (DB::isError($result)){
die("Could not query the database: <br />".$query." ".DB::errorMessage($result));
}
while ($result_row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
$test[]=$result_row;
}
$smarty->assign('posts',$test);
$smarty->assign('owner_id',$_SESSION["user_id"]);
$query = "SELECT * FROM `users` NATURAL JOIN `comments` WHERE `post_id`=$post_id";
$result = $connection->query($query);
if (DB::isError($result)){
die("Could not query the database: <br />".$query." ".DB::errorMessage($result));
}
$comment_count = $result->numRows();
while ($result_row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
$comments[] = $result_row;
}
$smarty->assign('posts',$test);
$smarty->assign('comments',$comments);
$smarty->assign('comment_count',$comment_count);
$smarty->display('view_post.tpl');
$connection->disconnect();
// Display the footer
$smarty->display('footer.tpl');
?>
Posted by krautgrrl on 09/27 at 11:46 AM
Chapter 16 Code • (13) Comments • (65) Trackbacks • Permalink
Chapter 16 Code • (13) Comments • (65) Trackbacks • Permalink
Statistics
This page has been viewed 375314 times
Page rendered in 0.2001 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: 8
Total anonymous users: 0
Most Recent Visitor on: 02/11/2012 02:16 am
The most visitors ever was 1103 on 11/20/2007 12:50 pm
