MySQL database functions (2)

Source: Internet
Author: User

(6) int mysql_db_query (string db_name, string query [, int link_id]);

In addition to extracting an additional database name parameter, mysql_db_query () makes it the default database before executing the query, similar to mysql_query.

<? Php

$ Link = mysql_pconnect ("localhost", "sunsoft", "suixiang ")
Or die ("cocould not connect ");

Print ("Connected successfully ");

$ Query = "SELECT * FROM president ";

$ Result = mysql_db_query ("samp_db", $ query) or die ("Query failed ");


?>

(7) int mysql_drop_db (string db_name, int [link_id]);

Tells the MySQL server identified by link_id to delete the database with the given name. If the database is successfully deleted, true is returned. If an error occurs, false is returned. You must have the DROP permission to delete the database.
Be careful with this function. If you delete a database, it does not exist and cannot be recovered.
Mysql_query () is more suitable for publishing the drop database statement using mysql_drop_db.

<? Php

$ Link = mysql_pconnect ("localhost", "sunsoft", "suixiang ")
Or die ("cocould not connect ");

If (mysql_drop_db ("my_db "))

Print ("Database dropped successfullyn ");

Else

Printf ("Error dropping database: % sn", mysql_error ());

?>

(8) int mysql_errno (int [link_id]);

For a given connection, the error code of the MySQL-related function that contains the recently returned status is returned. Zero value indicates no error.

Example

<? Php

$ Link = mysql_pconnect ("localhost", "sunsoft", "suixiang ")
Or die ("cocould not connect ");

Print ("Connected successfully ");

$ Query = "SELECT * FROM president ";

$ Result = mysql_query ($ query) or die ("Query failed, error code =". mysql_errno ());


?>
(9) string mysql_error (int [link_id]);

For a given connection, an error message string containing the MySQL-related functions in the recently returned status is returned. Null indicates that no error has occurred.

<? Php

$ Link = mysql_pconnect ("localhost", "sunsoft", "suixiang ")
Or die ("cocould not connect ");

Print ("Connected successfully ");

$ Query = "SELECT * FROM president ";

$ Result = mysql_query ($ query) or die ("query failed, error message =". mysql_error ());


?>

(10) array mysql_fetch_array (int result, int [result_typ]);

This function is used to split the query result into array variables. If the result contains no data, the return value is false. This function can be called the Enhanced Function of mysql_fetch_row (). In addition to putting the return column and numeric index into the array, you can also put the text index into the array. If multiple return columns are of the same text name, the last column is valid. The solution is to use a digital index or column with the same name) alias (alias ). It is worth noting that the processing speed of this function is not much slower than that of mysql_fetch_row (). which function is used depends on the usage requirement. The result_typ parameter is a constant value and has the following constants: MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH.


Example

<? Php

$ Link = mysql_pconnect ("localhost", "sunsoft", "suixiang ")
Or die ("cocould not connect ");

Mysql_select_db ("stamp_db") or die ("cocould not select database ");


$ Query = "SELECT last_name, first_name FROM president ";

$ Result = mysql_query ($ query) or die ("Query failed ");

While ($ row = mysql_fetch_array ($ result ))

{

Printf ("% s <BR>

", $ Row [0], $ row [1]);

Printf ("% s <BR>

", $ Row [" last_name "], $ row [" first_name "]);

}

Mysql_free_result ($ result );

?>



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.