PHP4 and MySQL database operation functions (3) _ MySQL

Source: Internet
Author: User
7) database information functions (2 functions): 1. mysql_fetch_field () format: objectmysql_fetch_field (intquery, int [field_offset]); 1 object is returned, that is, a hash table marked: table: table name: Field name max_length: maximum length of the field not_null: If the field is notnull, 1 is returned; otherwise, 0 is returned. 7) database information function (2 ):

1. mysql_fetch_field ()
Format: object mysql_fetch_field (int query, int [field_offset]);

Returns an object, that is, a hash table marked:
Table: table name
Name: Field name
Max_length: maximum length of the field
Not_null: If the field is not null, 1 is returned; otherwise, 0 is returned.
Primary_key: If the field is primary key, 1 is returned; otherwise, 0 is returned.
Unique_key: If the field is unique key, 1 is returned; otherwise, 0 is returned.
Multiple_key: If the field is not a unique key, 1 is returned; otherwise, 0 is returned.
Numeric: If the field is numeric, 1 is returned; otherwise, 0 is returned.
Blob: If the field is blob, 1 is returned; otherwise, 0 is returned.
Type: field type
Unsigned: If the field is unsigned, 1 is returned; otherwise, 0 is returned.
Zerofill: If the field is zero filled, 1 is returned; otherwise, 0 is returned.

Reference format: Object name-> subscript name

You can use this function to obtain the table name, field name, and type .......

Example:

$ Query = mysql_query ($ SQL, $ connect );
While ($ object = mysql_fetch_field ($ query ))
{
Echo "table name:". $ object-> table ."
";
Echo "field name:". $ object-> name ."
";
Echo "primary key:". $ object-> primary_key ."
";
Echo "not null:". $ object-> not_null ."
";
Echo "field type:". $ object-> type ."
";
Echo "field max length:". $ object-> max_length ."
";
}
?>

Note: the hash table starts with 0 coordinates, that is, the first field is 0 items in the hash table.
If you want to directly obtain the information of the third field in the hash table, the format is as follows:
$ Query = mysql_query ($ SQL, $ connect );
$ Object = mysql_fetch_field ($ query, 2 );
Echo "table name:". $ object-> table ."
";
Echo "field name:". $ object-> name ."
";
Echo "primary key:". $ object-> primary_key ."
";
Echo "not null:". $ object-> not_null ."
";
Echo "field type:". $ object-> type ."
";
Echo "field max length:". $ object-> max_length ."
";
?>

In fact, this can also be achieved through the following function.

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

Move the cursor to the specified field.
Example:

$ Query = mysql_query ($ SQL, $ connect );
$ Seek = mysql_field_seek ($ query, 2 );
$ Object = mysql_fetch_field ($ query );
Echo "table name:". $ object-> table ."
";
Echo "field name:". $ object-> name ."
";
Echo "primary key:". $ object-> primary_key ."
";
Echo "not null:". $ object-> not_null ."
";
Echo "field type:". $ object-> type ."
";
Echo "field max length:". $ object-> max_length ."
";
?>

This also meets the same requirements as the above example.

8) database name and table name (2 ):

1. mysql_list_dbs ()
Format: int mysql_list_dbs (int link_identifier );
Obtain the names of all available databases ).

Example:

$ Connect = mysql_connect ($ host, $ usr, $ pwd );
$ Dbs = mysql_list_dbs ($ connect );
$ Rows = mysql_num_rows ($ dbs );
Echo "database total:". $ rows;
$ I = 0;
While ($ I <$ rows)
{
$ Db_name [$ I] = mysql_tablename ($ dbs, $ I );
Echo $ db_name [$ I];
$ I ++;
}
?>
The names of all databases in MySQL are displayed in sequence ).
Note: it is equivalent to the show databases command in MySQL.

2. mysql_list_tables ()
Format: int mysql_list_tables (string database name );
Displays the names of all tables in the database.

Example:

$ Connect = mysql_connect ($ host, $ usr, $ pwd );
$ Tables = mysql_list_tables ("mysql ");
$ Rows = mysql_num_rows ($ tables );
Echo "Table total:". $ rows;
$ I = 0;
While ($ I <$ rows)
{
$ Table_name [$ I] = mysql_tablename ($ tables, $ I );
Echo $ table_name [$ I];
$ I ++;
}

?>

The names of all tables under mysql are displayed in sequence.
Note: it is equivalent to the show tables command in MySQL (first use the use mysql command to select a database)

Reprint: www.chinaphp.com

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.