SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    MySQL data types using php

    Im currently building my own database admin programm and have hit a bit of stumbling block.
    Its nothing major at the moment but made me think.

    Does anyone have any ideas for outputting the mysql data types using php??

    I am currently using:

    $table = $_GET['table'];
    $result = mysql_query("SELECT * FROM $table");
    $fields = mysql_num_fields($result);
    $rows = mysql_num_rows($result);
    $table = mysql_field_table($result, 0);
    for ($i=0; $i < $fields; $i++)
    {
    $type = mysql_field_type($result, $i);
    $name = mysql_field_name($result, $i);
    $len = mysql_field_len($result, $i);
    $flags = mysql_field_flags($result, $i);
    }

    but all $type returns is int, real and blob.
    How do i get it to say varchar or double for instance??

    Anny assistance would be greatly appreciated

    cheers

    Shujunie

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Code:
    For php 4.3 and mysql 4.x the returned values are:
    
    CHAR, VARCHAR, ENUM, SET: string
    TINYINT, SMALLINT, MEDIUMINT, INT, INTEGER, BIGINT: int
    FLOAT, DOUBLE, DECIMAL, NUMERIC: real
    TIMESTAMP: timestamp
    YEAR: year
    DATE: date
    TIME: time
    DATETIME: datetime
    TINYBLOB, MEDIUMBLOB, LONGBLOB, BLOB, TINYTEXT, TEXT, MEDIUMTEXT, LONGTEXT: blob
    you may get better data with: SHOW FIELDS FROM table.db
    or http://dev.mysql.com/doc/refman/5.1/a/describe.html

    Also to note " Like mysql_fetch_field, this returns the PHP type, not the MySQL type."
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    try the INFORMATION_SCHEMA (mysql version 5)
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •