One table database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
earny
Forum Newbie
Posts: 5
Joined: Tue Jul 21, 2009 10:22 am

One table database

Post by earny »

I have a one table database with a title column. I am trying to make the titles appear dynamically with html link but i keep getting error in step 2: Could not locate the specified joke ID. I can clearly see the title in the mysql database and i am able to see all the contents of the table through a do while loop

the table has 5 columns: id, title, desciption, date, author. Here is the code below. Please help

Code: Select all

<?php 
 
include('config.php'); 
 
 
// Get the joke text from the database
$id = $_GET['id'];
$joke = @mysql_query(
    "SELECT title, id FROM joke WHERE id='$id'");
if (!$joke) {
  exit('Unable to load the joke from the database.');
}
 
if (mysql_num_rows($joke) > 1) {
  exit('Could not locate the specified joke ID.');
}
 
$joke = mysql_fetch_array($joke);
$title = $joke['title'];
 
// Filter out HTML code
$title = htmlspecialchars($title);
 
// If no page specified, default to the first page ($page = 0)
if (!isset($_GET['page'])) {
  $page = 0;
} else {
  $page = $_GET['page'];
}
 
// Split the text into an array of pages
$textarray = spliti('\[PAGEBREAK]', $title);
 
// Select the page we want
$title = $textarray[$page];
 
// Bold and italics
$title = str_replace(array('[b]', '[B]'), '<strong>', $title);
$title = str_replace(array('[eb]', '[EB]'), '</strong>', $title);
$title = str_replace(array('[i]', '[I]'), '<em>', $title);
$title = str_replace(array('[ei]', '[EI]'), '</em>', $title);
 
// Paragraphs and line breaks
$title = ereg_replace("\r\n", "\n", $title);
$title = ereg_replace("\r", "\n", $title);
$title = ereg_replace("\n\n", '</p><p>', $title);
$title = ereg_replace("\n", '<br />', $title);
 
// Hyperlinks
$title = eregi_replace(
  '\\[L]([-_./a-z0-9!&%#?+,\'=:;@~]+)\\[EL]',
  '<a href="\\1">\\1</a>', $title);
$title = eregi_replace(
  '\\[L=([-_./a-z0-9!&%#?+,\'=:;@~]+)]([^\\[]+)\\[EL]',
  '<a href="\\1">\\2</a>', $title);
 
$PHP_SELF = $_SERVER['PHP_SELF'];
 
if ($page != 0) {
  $prevpage = $page - 1;
  echo "<p><a href=\"$PHP_SELF?id=$id&page=$prevpage\">".
      'Previous Page</a></p>';
}
 
echo "<p>$title</p>";
 
if ($page < count($textarray) - 1) {
  $nextpage = $page + 1;
  echo "<p><a href=\"$PHP_SELF?id=$id&page=$nextpage\">".
      'Next Page</a></p>';
}
 
?>
Last edited by califdon on Wed Jul 22, 2009 8:21 pm, edited 1 time in total.
Reason: Moderator added proper tags to display PHP - please do likewise on future posts.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: One table database

Post by califdon »

Is your id field numeric? If so, remove the quotes around it in your SQL statement.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: One table database

Post by superdezign »

Code: Select all

if (mysql_num_rows($joke) > 1) {
  exit('Could not locate the specified joke ID.');
}
If the amount of rows is more than one, then exit with the message "Could not locate the specified joke ID."
Sounds fishy.

If you are ending up with more than one row from your query, then your id field isn't unique. It is impossible for a unique id to match more than one row. That defeats the purpose. Echo your query (with the value of $id included) and tell us what it says.

Also, don't suppress error messages when you are debugging.
earny
Forum Newbie
Posts: 5
Joined: Tue Jul 21, 2009 10:22 am

Re: One table database

Post by earny »

thanks a bunch, i got it fixed. One more question. This is my first database site i am building. It is a simple one

I am know in the process of adding dynamic meta tags and title. Here is what i have so far but it does not work. What mistakes am i making

<head>
<?php


include('config.php');
$id = $_GET['id'];
$seotags = @mysql_query(
"SELECT metag, title_post FROM joke WHERE id='$id'");
if (!$seotags) {
exit('Unable to load the joke from the database.');
}
?>

<title> <?php echo $title_Post; ?></title>
<META name="description" content= " <?php echo $metag; ?> ">

</head>

Here is the source result:
<head>


<title> </title>
<META name="description" content= " ">

</head>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: One table database

Post by califdon »

earny wrote:

Code: Select all

<?php 
 
 
include('config.php'); 
$id = $_GET['id'];
$seotags = @mysql_query(
    "SELECT metag, title_post FROM joke WHERE id='$id'");
if (!$seotags) {
  exit('Unable to load the joke from the database.');
}
?>
 
<title> <?php echo $title_Post; ?></title>
<META name="description" content= " <?php echo $metag; ?> ">
 
</head>
 
Here is the source result:
<head>
 
 
<title> </title>
<META name="description" content= "  ">
 
</head>
First, whenever you post code here in the forum, please enclose it in [ code=php ] ... [ /code ] tags, as I have done above, for readability.

You have two mistakes in your syntax. PHP is case sensitive when it comes to variable names, so $title_Post is not the same as $title_post.

Even more critical, when you execute mysql_query(), it assigns a resource pointer to the value $seotags, but that does not assign values to any other variables, such as $title_post or $metag. In general, the results of your query could be thousands of rows! You have to call mysql_fetch_... (_array, _assoc, _row or whatever) and THEN assign values from the array that is returned. See http://www.w3schools.com/PHP/func_mysql_fetch_array.asp.
Post Reply