Jump to content

determining field type


jeff5656

Recommended Posts

With this code I can determine the fieldname and the value.  They are $col_key and $col_value respectively:

$sql = "SELECT * FROM $tbl ";
$result = mysql_query($sql)
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
if ($row == 1) {
	foreach ($line as $col_key => $col_value) {

 

But how do I determine the field TYPE (i.e. varchar, text, date etc)?

Link to comment
Share on other sites

Ok when I put field type in to this code:

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
if ($row == 1) {

	foreach ($line as $col_key => $col_value) {
		echo "<b>".$col_key. ":</b>   ";
		echo "field is ".mysql_field_type ($result, $col_key);
		?><input type="<?php if ($col_key=='password'){echo 'password';} else {echo 'text';}?>"  name="<?php echo $col_key;?>" value="<?php echo $col_value;?>" /><br/><?php 

	}

}

 

all I get is "field type is int"  for every field, even ones that are NOT int.

Link to comment
Share on other sites

You can use this query:

SHOW FIELDS FROM table

No i want the field type for each field as I loop through them (I'm making a generic form so I want the input types to reflect the field type - so a text box will be used if the field type is text etc)

 

That query is the only way you're going to find out its type. You'll have to run that query ahead of time, make an associative array of the field names and their types, and then use that later when you're looping through results.

Link to comment
Share on other sites

 

That query is the only way you're going to find out its type. You'll have to run that query ahead of time, make an associative array of the field names and their types, and then use that later when you're looping through results.

ugh. So theres no way to identify a field type inside the loop, field by field??

Link to comment
Share on other sites

 

That query is the only way you're going to find out its type. You'll have to run that query ahead of time, make an associative array of the field names and their types, and then use that later when you're looping through results.

ugh. So theres no way to identify a field type inside the loop, field by field??

 

No, but you could do something like this.

$result = mysql_query("SELECT * FROM $tbl");

$fields = array();
while($row = mysql_fetch_assoc($result)) {
$fields[$row['Field']] = $row['Type'];
}

$sql = "SELECT * FROM $tbl ";
$result = mysql_query($sql)
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
if ($row == 1) {	
	foreach ($line as $col_key => $col_value) {
		echo "<b>".$col_key. ":</b>   ";
		echo "field is ".$fields[$col_key];
		?><input type="<?php if ($col_key=='password'){echo 'password';} else {echo 'text';}?>"  name="<?php echo $col_key;?>" value="<?php echo $col_value;?>" /><br/><?php 

	}		
}
}

 

yes with mysql_field_type() try it using the fresh $result loop not within the foreach()

 

mysql_field_type does not return the actual field type, just a generalization. For example, tinyint, smallint, mediumint, int, and bigint are all classified as "int".

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.