search
HomeBackend DevelopmentPHP TutorialMySQL related instructions_PHP tutorial

MySQL related instructions_PHP tutorial

Jul 21, 2016 pm 03:57 PM
mysqltwohandleexistdatabaseyesmoduleofRelatedtypeillustrateresourceconnect

Resource Types
There are two resource types used in the MySQL module. The first is the connection handle of the database, and the second is the result set returned by the SQL query.

Predefined constants
The following constants are defined by this extension module, so they are only valid after this extension module is compiled into PHP or is dynamically loaded at runtime.

In PHP 4.3.0 and later versions, more client tags are allowed to be specified in the mysql_connect() function and mysql_pconnect() function. The defined constants are listed below:

Table 2. MySQL client constants

Constant Description
MYSQL_CLIENT_COMPRESS uses compressed communication protocol
MYSQL_CLIENT_IGNORE_SPACE allows spaces after the function name
MYSQL_CLIENT_INTERACTIVE allows setting the interactive_timeout time to wait idle before disconnecting (instead of wait_timeout).
MYSQL_CLIENT_SSL uses SSL encryption. This flag is only available when the MySQL client library version is 4.x or higher. Both PHP 4 and Windows version of PHP 5 installation packages are bundled with 3.23.x. The


mysql_fetch_array() function uses a constant to represent the type of the returned array. The following is the definition of constants:

Table 3. MySQL fetch constants

Constant Description
MYSQL_ASSOC The data column returned uses the field name as the index name of the array.
MYSQL_BOTH The data column returned uses the field name and numeric index as the index name of the array.
MYSQL_NUM The data column returned uses a numeric index as the index name of the array. The index starts at 0 , indicating the first field of the returned result.


Notes
Note: Most MySQL functions accept link_identifier as the last optional parameter. If this parameter is not provided, the last open connection is used. If a connection does not exist, the default parameters defined in php.ini will be used to try to establish a connection. If the connection is unsuccessful, the function returns FALSE.

Example
The following simple example demonstrates how to connect to the database, execute query statements, print the returned result set and disconnect from the database and a series of basic MySQL operations. 例子 1. MySQL 例子

// 连接,选择数据库
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
   or die('Could not connect: ' . mysql_error());
echo 'Connected successfully';
mysql_select_db('my_database') or die('Could not select database');

// 执行 SQL 查询
$query = 'Select * FROM my_table';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

// 用 HTML 显示结果
echo "

n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
   echo "tn";
   foreach ($line as $col_value) {
       echo "ttn";
   }
   echo "tn";
}
echo "
$col_value
n";

// Release the result set
mysql_free_result($result);

// Close the connection
mysql_close($link ; User
mysql_client_encoding -- Returns the name of the character set
mysql_close -- Closes the MySQL connection
mysql_connect -- Opens a connection to the MySQL server
mysql_create_db -- Creates a new MySQL database
mysql_data_seek -- Move the internal result pointer
mysql_db_name -- Get the result data
mysql_db_query -- Send a MySQL query
mysql_drop_db -- Drop (delete) a MySQL database
mysql_errno -- Return the previous MySQL operation Numeric encoding of the error message
mysql_error -- Returns the text error message generated by the previous MySQL operation
mysql_escape_string -- Escapes a string for mysql_query
mysql_fetch_array -- Gets a row from the result set as an associative array, Or a numeric array, or both
mysql_fetch_assoc -- Get a row from the result set as an associative array
mysql_fetch_field -- Get column information from the result set and return it as an object
mysql_fetch_lengths -- Get each row in the result set The length of the output
mysql_fetch_object -- Get a row from the result set as an object
mysql_fetch_row -- Get a row from the result set as an enumeration array
mysql_field_flags -- Get the flags associated with the specified field from the result
mysql_field_len -- Return the length of the specified field
mysql_field_name -- Get the field name of the specified field in the result
mysql_field_seek -- Set the pointer in the result set to the specified field offset
mysql_field_table -- Get the specified The name of the table where the field is located
mysql_field_type -- Get the type of the specified field in the result set
mysql_free_result -- Release the result memory
mysql_get_client_info -- Get the MySQL client information
mysql_get_host_info -- Get the MySQL host information
mysql_get_proto_info -- Get MySQL protocol information
mysql_get_server_info -- Get MySQL server information
mysql_info -- Get the latest query information
mysql_insert_id -- Get the ID generated by the previous Insert operation
mysql_list_d bs -- List all databases in the MySQL server
mysql_list_fields -- List the fields in the MySQL results
mysql_list_processes -- List the MySQL processes
mysql_list_tables -- List the tables in the MySQL database
mysql_num_fields -- Get the number of fields in the result set
mysql_num_rows -- Get the number of rows in the result set
mysql_pconnect -- Open a persistent connection to the MySQL server
mysql_ping -- Ping a server connection, reconnect if there is no connection
mysql_query -- Send a MySQL query
mysql_real_escape_string -- Escape special characters in the string used in the SQL statement, taking into account the current character set of the connection
mysql_result -- Get the result data
mysql_select_db -- Select the MySQL database
mysql_stat -- Get the current system status
mysql_tablename -- Get the table name
mysql_thread_id -- Return the ID of the current thread
mysql_unbuffered_query -- Send a SQL query to MySQL and not Line to get and cache results




http://www.bkjia.com/PHPjc/317799.html

www.bkjia.com
true

http: //www.bkjia.com/PHPjc/317799.htmlTechArticleResource Types There are two resource types used in the MySQL module. The first is the connection handle of the database, and the second is the result set returned by the SQL query. Predefined constants The following constants are modeled by this extension...
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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

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 Article

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

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

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.