PHP4 and MySQL database operation function detailed explanation (v)

Source: Internet
Author: User
Tags hash numeric connect mysql query table name mysql database

(7) Database information functions (2):

1, Mysql_fetch_field ()
Format: Object Mysql_fetch_field (int query, int [field_offset]);

Returns 1 objects, a hash table, labeled:
Table: Tables Name
Name: Field name
Max_length: Maximum length of this field
Not_null: Field NOT NULL returns 1, otherwise returns 0
Primary_key: Field Primary key returns 1, otherwise returns 0
Unique_key: Field Unique key returns 1, otherwise returns 0
Multiple_key: field is not unique key returns 1, otherwise returns 0
Numeric: Field numeric returns 1, otherwise returns 0
Blob: Field BLOB returns 1, otherwise returns 0
Type: Types of fields
Unsigned: Field unsigned returns 1, otherwise returns 0
Zerofill: Field Zero filled returns 1, otherwise returns 0

The reference format is: Object name-> subscript name

Use this function to get the table name, field name, type ....

Example:

<?php
$query = mysql_query ($sql, $connect);
while ($object = Mysql_fetch_field ($query))
{
echo "Table name:". $object->table. " <br> ";
echo "Field name:". $object->name. " <br> ";
echo "PRIMARY key:". $object->primary_key. " <br> ";
echo "NOT null:". $object->not_null. " <br> ";
echo "field type:". $object->type. " <br> ";
echo "Field max Length:". $object->max_length. " <br> ";
}
?>

Note: The hash table starts with 0 coordinates, that is, the first field is the 0 items in the Hashtable.
If we want to get directly to the third or third field of the hash table, you can use the following format:
<?php
$query = mysql_query ($sql, $connect);
$object = Mysql_fetch_field ($query, 2);
echo "Table name:". $object->table. " <br> ";
echo "Field name:". $object->name. " <br> ";
echo "PRIMARY key:". $object->primary_key. " <br> ";
echo "NOT null:". $object->not_null. " <br> ";
echo "field type:". $object->type. " <br> ";
echo "Field max Length:". $object->max_length. " <br> ";
?>

In fact, this can also be done by the following function to achieve the same goal.

2, Mysql_field_seek ()
Format: int mysql_field_seek (int $query, int field_offset);

Moves the cursor over the specified field.
Example:

<?php
$query = mysql_query ($sql, $connect);
$seek = Mysql_field_seek ($query, 2);
$object = Mysql_fetch_field ($query);
echo "Table name:". $object->table. " <br> ";
echo "Field name:". $object->name. " <br> ";
echo "PRIMARY key:". $object->primary_key. " <br> ";
echo "NOT null:". $object->not_null. " <br> ";
echo "field type:". $object->type. " <br> ";
echo "Field max Length:". $object->max_length. " <br> ";
?>

This also meets the same requirements as the previous example.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.