Thread: no of columns

  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    chennai, tamil nadu, INDIA
    Rep Power
    how to find the number of columns for the particular table thru sql query?
  2. #2
  3. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Caro, Michigan
    Rep Power
    There are a couple ways you can do it.

    $result = mysql_query("SHOW COLUMNS FROM your_table");
    $num_colums = mysql_num_rows($result);


    $result = mysql_query("SELECT * FROM your_table LIMIT 1");
    $num_colums = mysql_num_fields($result);

    The show columns command gives you more into about what type the columns are, whether it's a key or not, etc. Try it from the command line. The second way is better if you're already selecting something from the database, then you can count the fields before you start processing them. One warning though, I'm not sure how it handles a null field in the result, it might not count it??? I'm not sure, the best way is to test...

    ---John Holmes...

    Since there were a couple ways to do it, I was nice, but I found all the answers in the manuals.

    * The manual can probably answer 90% of your questions...
    * PHP Manual.
    * MySQL Manual:

    [This message has been edited by SepodatiCreations (edited October 10, 2000).]

Similar Threads

  1. How to merge columns data into single CLOB column
    By javaq in forum Oracle Development
    Replies: 3
    Last Post: November 3rd, 2003, 10:05 AM
  2. LOAD DATA into table, ignoring some columns?
    By baddy in forum MySQL Help
    Replies: 1
    Last Post: September 18th, 2003, 12:35 PM
  3. Can't view MySQL table columns in Win installation
    By chaos theory in forum MySQL Help
    Replies: 0
    Last Post: August 15th, 2002, 08:00 PM
  4. Querying multiple columns
    By maxr in forum MySQL Help
    Replies: 2
    Last Post: April 10th, 2002, 03:17 PM
  5. show columns command
    By jiml in forum MySQL Help
    Replies: 0
    Last Post: June 7th, 2000, 12:45 AM

IMN logo majestic logo threadwatch logo seochat tools logo