Home php教程 php手册 MySQL相关说明

MySQL相关说明

Jun 13, 2016 pm 12:33 PM
mysql two handle exist database yes module of Related type illustrate resource connect

资源类型
在 MySQL 模块中使用了两种资源类型。第一种是数据库的连接句柄,第二种是 SQL 查询返回的结果集。 

预定义常量
以下常量由本扩展模块定义,因此只有在本扩展模块被编译到 PHP 中,或者在运行时被动态加载后才有效。

在 PHP 4.3.0 以后的版本中,允许在 mysql_connect() 函数和 mysql_pconnect() 函数中指定更多的客户端标记。下面列出所定义的常量: 

表格 2. MySQL 客户端常量

常量 描述 
MYSQL_CLIENT_COMPRESS 使用压缩的通讯协议 
MYSQL_CLIENT_IGNORE_SPACE 允许在函数名后留空格位 
MYSQL_CLIENT_INTERACTIVE 允许设置断开连接之前所空闲等候的 interactive_timeout 时间(代替 wait_timeout)。 
MYSQL_CLIENT_SSL 使用 SSL 加密。本标志仅在 MySQL 客户端库版本为 4.x 或更高版本时可用。在 PHP 4 和 Windows 版的 PHP 5 安装包中绑定的都是 3.23.x。  


mysql_fetch_array() 函数使用一个常量来表示所返回数组的类型。下面是常量的定义: 

表格 3. MySQL fetch 常量

常量 描述 
MYSQL_ASSOC 返回的数据列使用字段名作为数组的索引名。  
MYSQL_BOTH 返回的数据列使用字段名及数字索引作为数组的索引名。  
MYSQL_NUM 返回的数据列使用数字索引作为数组的索引名。索引从 0 开始,表示返回结果的第一个字段。  


注释
注: 大多数 MySQL 函数都接受 link_identifier 作为最后一个可选参数。如果未提供此参数,则使用最后一个打开的连接。如果不存在连接,则会用 php.ini 中定义的默认参数去尝试建立连接。如果连接不成功,函数返回 FALSE。 

范例
下面的简单例子演示如何连接数据库,执行查询语句,打印返回结果集和断开数据库等一系列基本的 MySQL 操作。 例子 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 "\t\n";
   foreach ($line as $col_value) {
       echo "\t\t\n";
   }
   echo "\t\n";
}
echo "
$col_value
\n";

// 释放结果集
mysql_free_result($result);

// 关闭连接
mysql_close($link);
?>  



目录
mysql_affected_rows -- 取得前一次 MySQL 操作所影响的记录行数
mysql_change_user --  改变活动连接中登录的用户 
mysql_client_encoding -- 返回字符集的名称
mysql_close -- 关闭 MySQL 连接
mysql_connect -- 打开一个到 MySQL 服务器的连接
mysql_create_db -- 新建一个 MySQL 数据库
mysql_data_seek -- 移动内部结果的指针
mysql_db_name -- 取得结果数据
mysql_db_query -- 发送一条 MySQL 查询
mysql_drop_db -- 丢弃(删除)一个 MySQL 数据库
mysql_errno --  返回上一个 MySQL 操作中的错误信息的数字编码 
mysql_error --  返回上一个 MySQL 操作产生的文本错误信息 
mysql_escape_string --  转义一个字符串用于 mysql_query 
mysql_fetch_array --  从结果集中取得一行作为关联数组,或数字数组,或二者兼有 
mysql_fetch_assoc --  从结果集中取得一行作为关联数组 
mysql_fetch_field --  从结果集中取得列信息并作为对象返回 
mysql_fetch_lengths --  取得结果集中每个输出的长度 
mysql_fetch_object -- 从结果集中取得一行作为对象
mysql_fetch_row -- 从结果集中取得一行作为枚举数组
mysql_field_flags --  从结果中取得和指定字段关联的标志 
mysql_field_len --  返回指定字段的长度 
mysql_field_name --  取得结果中指定字段的字段名 
mysql_field_seek --  将结果集中的指针设定为制定的字段偏移量 
mysql_field_table --  取得指定字段所在的表名 
mysql_field_type --  取得结果集中指定字段的类型 
mysql_free_result -- 释放结果内存
mysql_get_client_info -- 取得 MySQL 客户端信息
mysql_get_host_info -- 取得 MySQL 主机信息
mysql_get_proto_info -- 取得 MySQL 协议信息
mysql_get_server_info -- 取得 MySQL 服务器信息
mysql_info --  取得最近一条查询的信息 
mysql_insert_id --  取得上一步 Insert 操作产生的 ID 
mysql_list_dbs --  列出 MySQL 服务器中所有的数据库 
mysql_list_fields -- 列出 MySQL 结果中的字段
mysql_list_processes -- 列出 MySQL 进程
mysql_list_tables -- 列出 MySQL 数据库中的表
mysql_num_fields -- 取得结果集中字段的数目
mysql_num_rows -- 取得结果集中行的数目
mysql_pconnect --  打开一个到 MySQL 服务器的持久连接 
mysql_ping -- Ping 一个服务器连接,如果没有连接则重新连接
mysql_query -- 发送一条 MySQL 查询
mysql_real_escape_string --  转义 SQL 语句中使用的字符串中的特殊字符,并考虑到连接的当前字符集 
mysql_result -- 取得结果数据
mysql_select_db -- 选择 MySQL 数据库
mysql_stat -- 取得当前系统状态
mysql_tablename -- 取得表名
mysql_thread_id -- 返回当前线程的 ID
mysql_unbuffered_query --  向 MySQL 发送一条 SQL 查询,并不获取和缓存结果的行 
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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Oracle's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

Explain the purpose of foreign keys in MySQL. Explain the purpose of foreign keys in MySQL. Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

Compare and contrast MySQL and MariaDB. Compare and contrast MySQL and MariaDB. Apr 26, 2025 am 12:08 AM

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

SQL vs. MySQL: Clarifying the Relationship Between the Two SQL vs. MySQL: Clarifying the Relationship Between the Two Apr 24, 2025 am 12:02 AM

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

What software is better for yi framework? Recommended software for yi framework What software is better for yi framework? Recommended software for yi framework Apr 18, 2025 pm 11:03 PM

Abstract of the first paragraph of the article: When choosing software to develop Yi framework applications, multiple factors need to be considered. While native mobile application development tools such as XCode and Android Studio can provide strong control and flexibility, cross-platform frameworks such as React Native and Flutter are becoming increasingly popular with the benefits of being able to deploy to multiple platforms at once. For developers new to mobile development, low-code or no-code platforms such as AppSheet and Glide can quickly and easily build applications. Additionally, cloud service providers such as AWS Amplify and Firebase provide comprehensive tools

How to safely store JavaScript objects containing functions and regular expressions to a database and restore? How to safely store JavaScript objects containing functions and regular expressions to a database and restore? Apr 19, 2025 pm 11:09 PM

Safely handle functions and regular expressions in JSON In front-end development, JavaScript is often required...

See all articles