Home Backend Development PHP Tutorial Basic knowledge of operating MYSQL with PHP

Basic knowledge of operating MYSQL with PHP

Jul 25, 2016 am 08:52 AM

  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 of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1658
14
PHP Tutorial
1257
29
C# Tutorial
1231
24
How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Apr 08, 2025 am 12:03 AM

There are four main error types in PHP: 1.Notice: the slightest, will not interrupt the program, such as accessing undefined variables; 2. Warning: serious than Notice, will not terminate the program, such as containing no files; 3. FatalError: the most serious, will terminate the program, such as calling no function; 4. ParseError: syntax error, will prevent the program from being executed, such as forgetting to add the end tag.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? Apr 09, 2025 am 12:09 AM

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

Explain Arrow Functions (short closures) introduced in PHP 7.4. Explain Arrow Functions (short closures) introduced in PHP 7.4. Apr 06, 2025 am 12:01 AM

The arrow function was introduced in PHP7.4 and is a simplified form of short closures. 1) They are defined using the => operator, omitting function and use keywords. 2) The arrow function automatically captures the current scope variable without the use keyword. 3) They are often used in callback functions and short calculations to improve code simplicity and readability.

See all articles