search
HomeBackend DevelopmentPHP TutorialPHP database operation code collection

  1. $conn = @mysql_connect("localhost", "username", "password") or die("Cannot connect to Mysql Server");
Copy code

Note: The closed connection must be displayed when using this connection

2. mysql_pconnect()-Establish a database connection Format: resource mysql_pconnect([string hostname [:port] [:/path/to/socket] [, string username] [, string password]]) example:

  1. $conn = @mysql_pconnect("localhost", "username", "password") or dir("Cannot connect to Mysql Server");
Copy code

Note: Using this connection function does not require closing the connection explicitly. It is equivalent to using a connection pool

3. mysql_close()-Close the database connection example:

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

4. mysql_select_db()-Select database Format: boolean mysql_select_db(string db_name [, resource link_id]) example:

  1. $conn = @mysql_connect("localhost", "username", "password") or die("Cannot connect to Mysql Server");
  2. @mysql_select_db("MyDatabase") or die("Cannot select This database, or the database does not exist");
Copy code

5. mysql_query()-Query MySQL Format: resource mysql_query (string query, [resource link_id]) example:

  1. $linkId = @mysql_connect("localhost", "username", "password") or die("Cannot connect to Mysql Server");
  2. @mysql_select_db("MyDatabase") or die("Cannot select This database, or the database does not exist");
  3. $query = "select * from MyTable";
  4. $result = mysql_query($query);
  5. mysql_close();
Copy code

Note: If SQL query If the execution is successful, the resource identifier is returned, and if it fails, FALSE is returned. If the update is executed successfully, it returns TRUE, otherwise it returns FALSE

6. mysql_db_query()-Query MySQL Format: resource mysql_db_query(string database, string query [, resource link_id]) example:

  1. $linkId = @mysql_connect("localhost", "username", "password") or die("Cannot connect to MysqlServer");
  2. $query = "select * from MyTable";
  3. $result = mysql_db_query("MyDatabase", $query);
  4. mysql_close();
Copy code

Note: In order to make the code clear, it is not recommended to use this function call

7. mysql_result()-obtain and display data Format: mixed mysql_result (resource result_set, int row [, mixed field]) example:

  1. $query = "select id, name from MyTable order by name";
  2. $result = mysql_query($query);
  3. for($count=0;$count{
  4. $c_id = mysql_result($result, 0, "id");
  5. $c_name = mysql_result($result, 0, "name");
  6. echo $c_id,$c_name;
  7. }
Copy the code

Description: The simplest and least efficient data acquisition function

8. mysql_fetch_row()-Get and display data Format: array mysql_fetch_row (resource result_set) example:

  1. $query = "select id, name from MyTable order by name";
  2. $result = mysql_query($query);
  3. while (list($id, $name) = mysql_fetch_row($result)) {
  4. echo("Name: $name ($id)
    ");
  5. }
Copy code

Description: The function gets the entire data row from result_set and puts the values ​​in an indexed array. Usually the list() function is used

9. mysql_fetch_array()-Get and display data Format: array mysql_fetch_array (resource result_set [, int result_type]) example:

  1. $query = "select id, name from MyTable order by name";
  2. $result = mysql_query($query);
  3. while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  4. $id = $row["id"];
  5. $name = $row["name"];
  6. echo "Name: $name ($id)
    ";
  7. }
Copy code

Another example:

  1. $query = "select id, name from MyTable order by name";
  2. $result = mysql_query($query);
  3. while($row = mysql_fetch_array($result, MYSQL_NUM)) {
  4. $id = $row[0];
  5. $name = $row[1];
  6. echo "Name: $name ($id)
    ";
  7. }
Copy code

Instructions: The values ​​of result_type are: MYSQL_ASSOC: The field name represents the key, and the field content is the value MYSQL_NUM: Numeric index array, the operation is the same as the mysql_fetch_ros() function MYSQL_BOTH: Returned both as an associative array and as a numerical index array. The default value of result_type.

10. mysql_fetch_assoc()-Get and display data Format: array mysql_fetch_assoc (resource result_set) Equivalent to calling mysql_fetch_array(resource, MYSQL_ASSOC);

11. mysql_fetch_object()-Get and display data Format: object mysql_fetch_object(resource result_set) example:

  1. $query = "select id, name from MyTable order by name";
  2. while ($row = mysql_fetch_object($result)) {
  3. $id = $row->id;
  4. $name = $ row->name;
  5. echo "Name: $name ($id)
    ";
  6. }
Copy code

Description: Returns an object, which is the same as mysql_fetch_array() in operation

12. mysql_num_rows()-the number of selected records Format: int mysql_num_rows(resource result_set) example:

  1. query = "select id, name from MyTable where id > 65";
  2. $result = mysql_query($query);
  3. echo "There are ".mysql_num_rows($result)." records with IDs greater than 65";
Copy code

Description: Only useful when determining the number of records obtained by select query.

13. mysql_affected_rows() - the number of records affected by Insert, update, delete Format: int mysql_affected_rows([resource link_id]) example:

  1. $query = "update MyTable set name='CheneyFu' where id>=5";
  2. $result = mysql_query($query);
  3. echo "The number of updated records with names with ID greater than or equal to 5 : ".mysql_affected_rows();
Copy code

Description: This function gets the number of rows affected by the INSERT, UPDATE or DELETE update statement

14. mysql_list_dbs()-Get database list information Format: resource mysql_list_dbs([resource link_id]) example:

  1. mysql_connect("localhost", "username", "password");
  2. $dbs = mysql_list_dbs();
  3. echo "Databases:
    ";
  4. while (list($db) = mysql_fetch_rows($dbs)) {
  5. echo "$db
    ";
  6. }
Copy code

Description: Display all database names

15. mysql_db_name()-Get the database name Format: string mysql_db_name(resource result_set, integer index) Description: This function obtains the database name located at the specified index in the result_set returned by mysql_list_dbs()

16. mysql_list_tables()-Get the list of database tables Format: resource mysql_list_tables(string database [, resource link_id]) example:

  1. mysql_connect("localhost", "username", "password");
  2. $tables = mysql_list_tables("MyDatabase");
  3. while (list($table) = mysql_fetch_row($tables)) {
  4. echo "$table
    ";
  5. }
Copy code

Description: This function gets the table names of all tables in the database

17. mysql_tablename()-Get the name of a database table Format: string mysql_tablename(resource result_set, integer index) example:

  1. mysql_connect("localhost", "username", "password");
  2. $tables = mysql_list_tables("MyDatabase");
  3. $count = -1;
  4. while (++$count echo mysql_tablename($tables, $count)."
    ";
  5. }
Copy code

Description: This function gets the table name located at the specified index in the result_set returned by mysql_list_tables()

18. mysql_fetch_field()-Get field information Format: object mysql_fetch_field(resource result [, int field_offset]) example:

  1. mysql_connect("localhost", "username", "password");
  2. mysql_select_db("MyDatabase");
  3. $query = "select * from MyTable";
  4. $result = mysql_query($query);
  5. $counts = mysql_num_fields($result);
  6. for($count = 0; $count $field = mysql_fetch_field($result, $count);
  7. echo "

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

    ";
  8. }
Copy code

Instructions: The returned object has a total of 12 object properties: 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, otherwise 0 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

19. mysql_num_fields()-Get the number of fields in the query Format: integer mysql_num_fields(resource result_set) example:

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

20. mysql_list_fields()-Get the field names of all fields in the specified table Format: resource mysql_list_fields (string database_name, string table_name [, resource link_id]) example:

  1. $fields =mysql_list_fields("MyDatabase", "MyTable");
  2. echo "The number of fields in the table MyTable in the database MyDatabase: ".mysql_num_fields($fields)."
    ";
Copy code

21, mysql_field_flags()-Get the specified field options Format: string mysql_field_flags (resource result_set, integer field_offset) example:

  1. $query = "select id, name from MyTable order by name";
  2. $result = mysql_query($query);
  3. $row=mysql_fetch_wor($row);
Copy code

22 , mysql_field_len()-Get the maximum length of the specified field Format: integer mysql_field_len (resource result_set, integer field_offset) example:

  1. $query = "select name from MyTable";
  2. $result = mysql_query($query);
  3. $row = mysql_fetch_row($result);
  4. echo mysql_field_len($result, 0)."
    ";
Copy code

illustrate: If mysql_field_len($reseult, 0) = 16777215 Then numer_format(mysql_field_len($result)) is equal to 16,777,215

23. mysql_field_name()-get the field name Format: string mysql_field_name (resource result_set, int field_offset) example:

  1. $query = "select id as PKID, name from MyTable order by name";
  2. $result = mysql_query($query);
  3. $row = mysql_fetch_row($result);
  4. echo mysql_field_name($result, 0); // Result: PKID
Copy code

24, mysql_field_type()-get the field type Format: string mysql_field_type (resource result_set, int field_offset) example:

  1. $query = "select id, name from MyTable order by name";
  2. $result = mysql_query($query);
  3. $row = mysql_fetch_row($result);
  4. echo mysql_field_type($result, 0) ; // Result: int
Copy code

25, mysql_field_table()-Get the table name where the field is located Format: string mysql_field_table (resource result_set, int field_offset) example:

  1. $query = "select id as PKID, name from MyTable order by name";
  2. $result = mysql_query($query);
  3. $row = mysql_fetch_row($result);
  4. 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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.