Home  >  Article  >  Backend Development  >  Basic knowledge of operating MYSQL with PHP

Basic knowledge of operating MYSQL with PHP

WBOY
WBOYOriginal
2016-07-25 08:52:13835browse
  1. mysql_connect()
  2. resource mysql_connect([string hostname [:port] [:/path/to/socket] [, string username] [, string password]])
  3. Example: $conn = @mysql_connect(" localhost", "username", "password") or dir("Cannot connect to Mysql Server");
  4. Using this connection must show the closing of the connection
Copy the code

Establish a database connection

  1. mysql_pconnect()
  2. resource mysql_pconnect([string hostname [:port] [:/path/to/socket] [, string username] [, string password]])
  3. Example: $conn = @mysql_pconnect("localhost" , "username", "password") or dir("Cannot connect to Mysql Server");
  4. Using this connection function does not require closing the connection explicitly. It is equivalent to using a connection pool
Copy code

Close the database connection

  1. mysql_close()
  2. $conn = @mysql_connect("localhost", "username", "password") or die("Cannot connect to Mysql Server");
  3. @mysql_select_db(" MyDatabase") or die("This database cannot be selected, or the database does not exist");
  4. echo "You have connected to the MyDatabase database";
  5. mysql_close();
Copy code

Select database

  1. mysql_select_db()
  2. boolean mysql_select_db(string db_name [, resource link_id])
  3. $conn = @mysql_connect("localhost", "username", "password") or die(" Cannot connect to Mysql Server");
  4. @mysql_select_db("MyDatabase") or die("This database cannot be selected, or the database does not exist");
Copy code

Query MySQL

  1. mysql_query()
  2. resource mysql_query (string query, [resource link_id])
  3. $linkId = @mysql_connect("localhost", "username", "password") or die("Cannot connect to Mysql Server");
  4. @ mysql_select_db("MyDatabase") or die("This database cannot be selected, or the database does not exist");
  5. $query = "select * from MyTable";
  6. $result = mysql_query($query);
  7. mysql_close();
  8. if If the SQL query is executed successfully, the resource identifier will be returned, and if it fails, FALSE will be returned. If the update is executed successfully, it returns TRUE, otherwise it returns FALSE
Copy code

Query MySQL

  1. mysql_db_query()
  2. resource mysql_db_query(string database, string query [, resource link_id])
  3. $linkId = @mysql_connect("localhost", "username", "password") or die("Cannot Connect to MysqlServer");
  4. $query = "select * from MyTable";
  5. $result = mysql_db_query("MyDatabase", $query);
  6. mysql_close();
  7. In order to make the code clear, it is not recommended to use this function call
Copy code

Get and display data

  1. mysql_result()
  2. mixed mysql_result (resource result_set, int row [, mixed field])
  3. $query = "select id, name from MyTable order by name";
  4. $result = mysql_query($query);
  5. $c_id = mysql_result($result, 0, "id");
  6. $c_name = mysql_result($result, 0, "name");
  7. The simplest, It is also the least efficient data acquisition function
Copy code

Get and display data

  1. mysql_fetch_row()
  2. array mysql_fetch_row (resource result_set)
  3. $query = "select id, name from MyTable order by name";
  4. $result = mysql_query($query);
  5. while (list($id, $name) = mysql_fetch_row($result)) {
  6. echo("Name: $name ($id)
    ");
  7. }
  8. The function gets the entire data row from result_set and places the values ​​in an indexed array. Usually the list() function is used
Copy code

Get and display data

  1. mysql_fetch_array()
  2. array mysql_fetch_array (resource result_set [, int result_type])
  3. $query = "select id, name from MyTable order by name";
  4. $resul t = mysql_query($query);
  5. while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  6. $id = $row["id"];
  7. $name = $row["name"];
  8. echo "Name: $name ($id)
    ";
  9. }
  10. result_type values ​​are:
  11. MYSQL_ASSOC: The field name represents the key, and the field content is the value
  12. MYSQL_NUM: Numeric index array, the operation is the same as the mysql_fetch_ros() function
  13. MYSQL_BOTH : Returned both as an associative array and as a numeric index array. The default value of result_type.
Copy code

Get and display data

  1. mysql_fetch_assoc()
  2. array mysql_fetch_assoc (resource result_set)
  3. Equivalent to calling mysql_fetch_array(resource, MYSQL_ASSOC);
Copy code

Get and display data

  1. mysql_fetch_object()
  2. object mysql_fetch_object(resource result_set)
  3. $query = "select id, name from MyTable order by name";
  4. while ($row = mysql_fetch_object($result)) {
  5. $id = $row->id ;
  6. $name = $row->name;
  7. echo "Name: $name ($id)
    ";
  8. }
  9. Is the same as mysql_fetch_array() in operation
Copy code

Selected records

  1. mysql_num_rows()
  2. int mysql_num_rows(resource result_set)
  3. #query = "select id, name from MyTable where id > 65";
  4. $result = mysql_que ry($query );
  5. echo "There are ".mysql_num_rows($result)." records with IDs greater than 65";
  6. This is only useful when determining the number of records obtained by the select query.
Copy code

Affected records

  1. mysql_affected_rows()
  2. int mysql_affected_rows([resource link_id])
  3. $query = "update MyTable set name="Che neyFu" where id> =5";
  4. $result = mysql_query($query);
  5. echo "The number of updated records with names with ID greater than or equal to 5:".mysql_affected_rows();
  6. This function obtains the number of records affected by the INSERT, UPDATE or DELETE update statement Number of lines
Copy code

Get database list information

  1. mysql_list_dbs()
  2. resource mysql_list_dbs([resource link_id])
  3. mysql_connect("local host", "username", "password" );
  4. $dbs = mysql_list_dbs();
  5. echo "Databases:
    ";
  6. while (list($db) = mysql_fetch_rows($dbs)) {
  7. echo "$db
    ";
  8. }
Copy code

Get the database name

  1. mysql_db_name()
  2. string mysql_db_name(resource result_set, integer index)
  3. This function gets the mys In the result_set returned by ql_list_dbs() The database name located at the specified index index
Copy code

Get the database table list

  1. mysql_list_tables()
  2. resource mysql_list_tables(string database [, resource link_id])
  3. mysql_ connect("localhost" , "username", "password");
  4. $tables = mysql_list_tables("MyDatabase");
  5. while (list($table) = mysql_fetch_row($tables)) {
  6. echo "$table
    ";
  7. }This function gets the table names of all tables in the database
Copy code

Gets the database table names

  1. mysql_tablename()
  2. string mysql_tablename(resource result_set, integer index)
  3. mysql_connect( "localhost", "username", "password");
  4. $tables = mysql_list_tables("MyDatabase");
  5. $count = -1;
  6. while (++$count < mysql_numrows($tables)) {
  7. echo mysql_tablename ($tables, $count)."
    ";
  8. }
  9. This function gets the table name located at the specified index in the result_set returned by mysql_list_tables()
Copy code

Get field information

  1. mysql_fetch_field()
  2. object mysql_fetch_field(resource result [, int field_offset])
  3. mysql_connect("localhost", "username", "password");
  4. mysql_ select_db("MyDatabase ");
  5. $query = "select * from MyTable";
  6. $result = mysql_query($query);
  7. $fields = mysql_num_fields($result);
  8. for($count = 0; $count < $fieds; $ count++) {
  9. $field = mysql_fetch_field($result, $count);
  10. echo "

    $field->name $field->type ($field->max_length)

    ";
  11. }
Copy code

The returned object has a total of 12 object attributes: name: field name table: the table where the field is located max_length: the maximum length of the field not_null: 1 if the field cannot be null, 0 otherwise primary_key: 1 if the field is the primary key, 0 otherwise unique_key: 1 if the field is a unique key, 0 otherwise multiple_key: 1 if the field is non-unique, 0 otherwise numeric: 1 if the field is numeric, 0 otherwise blob: 1 if the field is a BLOB, 0 otherwise type: the data type of the field unsigned: 1 if the field is an unsigned number, 0 otherwise zerofill: 1 if the field is "zero filled", 0 otherwise

Get the number of fields in the query

  1. mysql_num_fields()
  2. integer mysql_num_fields (resource result_set)
  3. $query = "select id, name from MyTable order by name";
  4. $result = mysql_query ($query );
  5. echo "The number of fields in this query is: ".mysql_num_fields($result)."
    ";
Copy code

Return the number of fields in the query result_set

Get the field names of all fields in the specified table

  1. mysql_list_fields()
  2. resource mysql_list_fields (string database_name, string table_name [, resource link_id])
  3. $fields = mysql_list_fields("MyDatabase", " MyTable ");
  4. echo "The number of fields in the table MyTable in the database MyDatabase: ".mysql_num_fields($fields)."
    ";
Copy code

Get the specified field option

  1. mysql_field_flags()
  2. string mysql_field_flags (resource result_set, integer field_offset)
Copy code

Get the maximum length of the specified field

  1. mysql_field_len()
  2. integer mysql_field_len (resource result_set, integer field_offset)
  3. $query = "select name from MyTable";
  4. $result = mysql_query($query);
  5. $row = mysql_fetch_row($result);
  6. echo mysql_field_len($result, 0)."< ;br />";
  7. If mysql_field_len($reseult, 0) = 16777215
  8. then numer_format(mysql_field_len($result)) is equal to 16,777,215
Copy code

Get the field name

  1. mysql_field_name()
  2. string mysql_field_name (resource result_set, int field_offset)
  3. $query = "select id as PKID, name from MyTable order by name";
  4. $result = mysql_query($query);
  5. $row = mysql_fetch_row($result ; string mysql_field_type (resource result_set, int field_offset)
  6. $query = "select id, name from MyTable order by name";
  7. $result = mysql_query($query);
$row = mysql_fetch_row($result);
echo mysql_field_type($result, 0); // Result: int

Copy code

Get the table name where the field is located
  1. mysql_field_table()
  2. string mysql_field_table (resource result_set, int field_offset)
  3. $query = "select id as PKID, name from MyTable order by name";
  4. $result = mysql_query($query);
$row = mysql_fetch_row($result);
echo mysql_field_table($result, 0); // Result: MyTable
Copy code

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn