HomePHP Page 4 - Building A Quick-And-Dirty PHP/MySQL Publishing System
Bedtime Stories - PHP
Looking to quickly add a dynamic news page to your corporate orpersonal Web site? This article shows you how, combining PHP's rapidapplication development capabilities with a MySQL database to create aprimitive news publishing system and administration module in just underfour hours.
You'll see, from the code on the previous page, that every press release title is linked to a script named "story.php" via its unique ID. This "story.php" script displays the complete text for the selected press release - and it looks like this:
<?
// includes
include("../conf.php");
include("../functions.php");
// open database connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to
connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// generate and execute query
$query = "SELECT slug, content, contact, timestamp FROM news WHERE id =
'$id'";
$result = mysql_query($query) or die ("Error in query: $query. " .
mysql_error());
// get resultset as object
$row = mysql_fetch_object($result);
// print details
if ($row)
{
?>
<p>
<b><? echo $row->slug; ?></b>
<p>
<font size="-1"><? echo nl2br($row->content); ?></font>
<p>
<font size="-2">This press release was published on <? echo
formatDate($row->timestamp); ?>. For more information, please contact <?
echo $row->contact; ?></font>
<?
}
else
{
?>
<p>
<font size="-1">That press release could not be located in our
database.</font>
<?
}
// close database connection
mysql_close($connection);
?>
<!-- page footer - snip -->
Again, extremely simple - connect, use the ID to get the full
text for the corresponding item, and display it. Here's what it looks like:
With that, I've successfully completed the first part of this development effort. I now have a primitive publishing system that can be used to provide users of a Web site with news, press releases and other information.