Thread: no of columns

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

    Join Date
    Aug 2000
    Location
    chennai, tamil nadu, INDIA
    Posts
    27
    Rep Power
    0
    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
    Location
    Brussels, Belgium
    Posts
    14,643
    Rep Power
    4492
    There are a couple ways you can do it.

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

    Or...

    $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. www.php.net/manual
    * MySQL Manual: www.mysql.com/documentation/mysql/bychapter
    *************************************************************

    [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, 09:05 AM
  2. LOAD DATA into table, ignoring some columns?
    By baddy in forum MySQL Help
    Replies: 1
    Last Post: September 18th, 2003, 11:35 AM
  3. Can't view MySQL table columns in Win installation
    By chaos theory in forum MySQL Help
    Replies: 0
    Last Post: August 15th, 2002, 07:00 PM
  4. Querying multiple columns
    By maxr in forum MySQL Help
    Replies: 2
    Last Post: April 10th, 2002, 02:17 PM
  5. show columns command
    By jiml in forum MySQL Help
    Replies: 0
    Last Post: June 6th, 2000, 11:45 PM

IMN logo majestic logo threadwatch logo seochat tools logo