Database Articles
  Home arrow Database Articles arrow Page 2 - PHP/MySQL News with Comments
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Forums Sitemap 
Download TestComplete 
JMSL Numerical Library 
IBM® developerWorks
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DATABASE ARTICLES

PHP/MySQL News with Comments
By: Matt Wade
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2002-09-03

    Table of Contents:
  • PHP/MySQL News with Comments
  • The Tables
  • Displaying the news
  • Displaying the comments
  • Displaying one item with comments
  • Adding comments
  • Summing it up and final script

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    PHP/MySQL News with Comments - The Tables


    (Page 2 of 7 )

    OK, as I mentioned just a moment ago we will need two tables for this news system. The first is simply called news. Here is the table structure.

    CREATE TABLE news (
       id int(10) unsigned NOT NULL auto_increment,
       postdate timestamp(14),
       title varchar(50) NOT NULL,
       newstext text NOT NULL,
       PRIMARY KEY (id),
       KEY postdate (postdate)
    );

    The first column, id, is an auto increment field. What that means to us is that as every row is inserted into the database, it will increase the number stored in that field by one. This is a very easy way to get a unique id. The three other fields should be fairly self explanatory. You will notice, however, that I am using a timestamp type for the postdate field. The reason I use a timestamp field is that when inserting, you don’t need to specify a value for the field. It will automatically set it to the current date and time. Simple huh?

    The second table we need is the comments table. Again the table structure.

    CREATE TABLE news_comments (
       id int(10) unsigned NOT NULL auto_increment,
       news_id int(10) unsigned DEFAULT '0' NOT NULL,
       name varchar(40) NOT NULL,
       comment text NOT NULL,
       PRIMARY KEY (id),
       KEY news_id (news_id)
    );

    We don’t see anything new here. I would like to comment on the KEY statements you see in both table structures though. Keys are a method of indexing the table. They allow for faster lookups. When you set a primary key, every value in that column must be unique. The other keys that I set, on postdate and news_id, are there to speed queries up. When we are displaying news, we are going to sort it by postdate. The key on that field will speed up the query. When we pull comments out of the database, we will look them up by the news_id they are associated with. Again, by adding a key on that column, we will speed queries up.

    More Database Articles Articles
    More By Matt Wade


       · dfsf
       · Thanks for a great tutorial.
       · hiI've search the web for ages to find a newssystem that does excatly what this...
       · I seem to be getting the problem where it's not showing the news table at all and...
       · sounds like it doesn't interact with your "news"-database as it...
       · file loaded: http://luna-illusions.co.uk/weetom/newsscript.phpThere's no design...
       · Hi.I just love this Tutorial and it works perfectly except for 1 thing. When you...
       · Warning: mysql_connect() [function.mysql-connect]: Access denied for user...
       · I've discovered a problem with this tutorial. The Comment can only be 500 Characters...
       · Tutorial is nice, but previous one was better, this one is just final script cut...
       · Hello Mr/Mrs Tutorial Creator (xD)I'm new toP and MySQL and I suck basically...
     

    DATABASE ARTICLES ARTICLES

    - More on Query Optimization for Oracle Databa...
    - Query Optimization in Oracle
    - Clusters and Other Data Structures for Oracle
    - Using Indexes with an Oracle Database
    - The Basics of Data Structures in Oracle
    - Oracle Data Structures
    - Best Practices for PL/SQL Variables
    - What`s Code Without Variables?
    - Clauses, Sorting, and SQL Queries
    - The From Clause and SQL Queries
    - Query Primer
    - Full Text Searches and Strings
    - Searching with Strings
    - Pattern Matching with Strings
    - Working with Cases of Strings





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT