string mysql_field_type(int result, int field_offset)
We will try to develop one script using our student table to display the field types of all the fields.
<?
$result = mysql_query ("SELECT * FROM student");
$no_of_fields = mysql_num_fields ($result);
$no_of_records = mysql_num_rows ($result);
$i = 0;
$table_name = mysql_field_table ($result, $i);
echo "Your '".$table_name."' table has ".$no_of_fields";
echo " fields and ".$no_of_records." records <BR>";
echo "The table has the following fields <BR>";
while ($i < $no_of_fields) {
// name of the field mysql_field_name
$name = mysql_field_name ($result, $i);
echo "Field name:<b>$name</b><br>";
// Field type field mysql_field_type
$type = mysql_field_type ($result, $i);
echo "Field type:$type<br>";
$i++;
}
?>
The above code will display the field type with the fields of the student table.
$select = $dbo->query('SELECT count(*) FROM student');
$meta = $select->getColumnMeta(0);
//var_dump($meta);
//print_r($meta);
while (list ($key, $val) = each ($meta)) {
echo "$key -> $val <br>";
}
$select = $dbo->prepare("SELECT COLUMN_NAME,COLUMN_TYPE, IS_NULLABLE, COLUMN_DEFAULT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'student' AND table_schema = 'sql_tutorial'");
$select->execute();
while($result=$select->fetch(PDO::FETCH_NUM)){
for($j=0;$j<4;$j++){
echo "$result[$j] > ";
}
echo "<br>";
}
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.