The following code produces this result from the Kevin Yank Book:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\program files\apache group\apache\htdocs\www\code\chapter7\joke.php on line 15
Back to the front page
THE CODE:
<!-- joke.php -->
<html>
<head>
<title> Joke </title>
</head>
<body>
<?php
$dbcnx = mysql_connect("localhost", "root", "mypassword");
mysql_select_db("jokes");
// Get the joke text from the database
$joke = mysql_query("SELECT JokeText FROM Jokes ".
"WHERE ID=$id");
$joke = mysql_fetch_array($joke);
$joketext = $joke["JokeText"];
// Filter out HTML code
$joketext = htmlspecialchars($joketext);
// If no page specified, default to the
// first page ($page = 0)
if (!isset($page)) $page = 0;
// Split the text into an array of pages
$textarray=split("[PAGEBREAK]",$joketext);
// Select the page we want
$joketext=$textarray[$page];
// Bold and italics
$joketext = eregi_replace("[b]","<b>",$joketext);
$joketext = eregi_replace("[eb]","</b>",$joketext);
$joketext = eregi_replace("[i]","<i>",$joketext);
$joketext = eregi_replace("[ei]","</i>",$joketext);
// Paragraphs and line breaks
$joketext = ereg_replace("\r","",$joketext);
$joketext = ereg_replace("\n\n","</p><p>",$joketext);
$joketext = ereg_replace("\n","<br />",$joketext);
// Hyperlinks
$joketext = ereg_replace(
"[L]([-./a-zA-Z0-9!&%#?,'=:~]+)[EL]",
"<a href=\"\1\">\1</a>", $joketext);
$joketext = ereg_replace(
"[L=([-./a-zA-Z0-9!&%#?,'=:~]+)]".
"([-_./a-zA-Z0-9 !&%#?,'=:~]+)[EL]",
"<a href=\"\1\">\2</a>", $joketext);
if ($page != 0) {
$prevpage = $page - 1;
echo("<p><a href=\"$PHP_SELF?id=$id&page=$prevpage\">".
"Previous Page</a></p>");
}
echo( "<p>$joketext</p>" );
if ($page < count($textarray) - 1) {
$nextpage = $page + 1;
echo("<p><a href=\"$PHP_SELF?id=$id&page=$nextpage\">".
"Next Page</a></p>");
}
?>
<p><a href="index.php">Back to the front page</a></p>
</body>
</html>