The mysql tutorial _ query () function executes a MySQL query.
Syntax
Mysql_query (query, connection) parameter description
Query is required. Specifies the SQL query to be sent. Note: The query string should not end with a semicolon.
Connection is optional. Specifies the SQL connection identifier. If not specified, the last opened connection is used.
Description
If no connection is enabled, this function will try to call the mysql_connect () function without parameters to establish a connection and use it.
Return Value
Mysql_query () Only returns a resource identifier for SELECT, SHOW, EXPLAIN or DESCRIBE statements. If the query execution is incorrect, FALSE is returned.
For other types of SQL statements, if mysql_query () is executed successfully, TRUE is returned. If an error occurs, FALSE is returned.
If the return value is not FALSE, the query is legal and can be executed by the server. This does not indicate any affected or returned number of rows. It is very likely that a query is successfully executed but does not affect or no rows are returned.
Instance
<? Php tutorial
$ Host = "mysql153.secureserver.net ";
$ Uname = "java2s ";
$ Pass = "password ";
$ Database = "java2s ";
$ Connection = mysql_connect ($ host, $ uname, $ pass) or die ("Database connection failed! ");
$ Result = mysql_select_db ($ database) or die ("Database cocould not be selected ");
$ Query = "drop table if exists mytable ";
$ Result = mysql_query ($ query) or die ("Query failed .");
?>
Detailed instance
<Html>
<Head>
<Title> Listing every database, table, and field </title>
</Head>
<Body>
<? Php
$ User = "java2s ";
$ Pass = "password ";
$ Db = "java2s ";
$ Link = mysql_connect ("mysql153.secureserver.net", $ user, $ pass );
If (! $ Link)
Die ("Couldn't connect to MySQL ");
$ Db_res = mysql_list_dbs ($ link );
While ($ db_rows = mysql_fetch_row ($ db_res )){
Print "<B> $ db_rows [0] </B> n ";
If (! @ Mysql_select_db ($ db_rows [0], $ link )){
Print "<dl> <dd> couldn't connect --". mysql_error (). "</dl> ";
Continue;
}
$ Tab_res = mysql_list_tables ($ db_rows [0], $ link );
Print "t <dl> <dd> n ";
While ($ tab_rows = mysql_fetch_row ($ tab_res )){
Print "t <B> $ tab_rows [0] </B> n ";
$ Query_res = mysql_query ("SELECT * from $ tab_rows [0]");
$ Num_fields = mysql_num_fields ($ query_res );
Print "tt <dl> <dd> n ";
For ($ x = 0; $ x <$ num_fields; $ x ++ ){
Print "tt <I> ";
Print mysql_field_type ($ query_res, $ x );
Print "</I> <I> ";
Print mysql_field_len ($ query_res, $ x );
Print "</I> <B> ";
Print mysql_field_name ($ query_res, $ x );
Print "</B> <I> ";
Print mysql_field_flags ($ query_res, $ x );
Print "</I> <br> n ";
}
Print "tt </d1> n ";
}
Print "t </d1> n ";
}
Mysql_close ($ link );
?>
</Body>
</Html>