Welcome guest! Login or Sign up!
Set my location
Home Ask Browse Photos Groups Advisors Widgets
Home » Databases » SELECT MySQL data type?

Select mysql data type?

Posted by jackola 3 months ago
Send me Private Mail | Report Abuse

is it possible for mysql to return a cell's data type? i want the value as well as what type of data it is (like INT, TEXT, etc)



I will post the answer to my own question, since I figured it out and other may want to know:

$table = array();
$query = 'DESCRIBE tablename';
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
$table[$row['Field']] = $row['Type'];
}

The above gets the Field names and Types in an array. Next you get the actual data:

$query = 'select * from tablename where ID = ' . $_GET['id'];
$result = mysql_query($query) or die(mysql_error());
$actualdata = mysql_fetch_assoc($result);

Now you filter through both together, matching up the field name with the field type and actual data:

foreach($table as $field=>$value) {
echo $actualdata[$field] . ' is the value of the ' . $field . ' field, which is type ' . $value . '.';
}

Hope this helps others!



Post an Answer:

Your Answer: HTML is not allowed.


Rss_feed    Myaol_cta1    Addtomyyahoo4    Add