PHP操作MySQL的mysql_fetch_* 函数的常见用法教程,
PHP操作MySQL的mysql_fetch_* 函数的常见用法教程,
mysql_fetch_* 列函数
mysql_fetch_* 列函数的主要功能是从查询返回的结果集中取得相关的查询结果,主要包括:
- mysql_fetch_array():从结果集中取得一行作为关联数组或索引数组,或二者兼有
- mysql_fetch_row():从结果集中取得一行作为枚举数组
- mysql_fetch_assoc():从结果集中取得一行作为关联数组
- mysql_fetch_object():从结果集中取得一行作为对象
- mysql_fetch_field():从结果集中取得字段信息并作为对象返回
- mysql_fetch_lengths():取得结果集中取得一行每个字段内容输出的长度
- mysql_fetch_array()
mysql_fetch_array() 函数用于从结果集中取得一行作为关联数组或索引数组,或二者兼有。成功返回一个数组,否则返回 FALSE 。
语法:
array mysql_fetch_array( resource result [, int result_type] )
参数说明:
- result:查询函数(如 mysql_query)返回的数据集资源
- result_type:可选常量,标明数组结果类型,可接受值如下:
- MYSQL_BOTH:默认,得到一个同时包含关联和数字索引的数组,用字段名作为键名
- MYSQL_ASSOC:只得到关联索引的数组
- MYSQL_NUM:只得到数字索引的数组
例子 1 ,使用 MYSQL_NUM :
<?php $conn = @mysql_connect("localhost","root","root123"); if (!$conn){ die("连接数据库失败:" . mysql_error()); } mysql_select_db("test", $conn); mysql_query("set character set 'gbk'"); $result = mysql_query("SELECT uid,username FROM user"); while($row = mysql_fetch_array($result, MYSQL_NUM)){ echo "用户ID:".$row[0]."<br />"; echo "用户名:".$row[1]."<br />"; } ?>
浏览器输出:
用户ID:1 用户名:admin 用户ID:2 用户名:小明 用户ID:3 用户名:Jack 用户ID:4 用户名:小王
例子 2 ,使用 MYSQL_ ASSOC :
//重复代码省略 $result = mysql_query("SELECT uid,username FROM user"); while($row = mysql_fetch_array($result, MYSQL_ ASSOC)){ echo "用户ID:".$row['uid']."<br />"; echo "用户名:".$row['username']."<br />"; }
浏览器输出内容同上。
当使用 MYSQL_BOTH 或省略该参数是,将同时具有 MYSQL_NUM 与 MYSQL_ ASSOC 的特性。
说明
本函数返回的字段名作为数组键值是区分大小写的
用 mysql_fetch_array() 并不明显 比用 mysql_fetch_row() 慢,而且还提供了明显更多的值
该函数只从当前数据指针取得一行数据作为结果返回,如果执行过一次,会将数据指针指向下一列数据
如果要取得多行或者全部数据,需要使用循环结构将数据逐行取出
如果结果中的两个或以上的列具有相同字段名,最后一列将优先。要访问同名的其它列,必须用该列的数字索引或给该列起个别名
mysql_fetch_row()
PHP 的 MySQL 操作函数 mysql_fetch_row() 用于从结果集中取得一行作为枚举数组。成功返回一个数组,否则返回 FALSE 。
语法:
array mysql_fetch_row( resource result )
该函数表现与 mysql_fetch_array( resource result, MYSQL_NUM ) 一致,请参考mysql_fetch_array() 函数用法,在此不在赘述。
mysql_fetch_object()
PHP 操作 MySQL 的函数 mysql_fetch_object() 用于从结果集中取得一行作为对象,成功返回一个对象,否则返回 FALSE 。
语法:
object mysql_fetch_object( resource result )
例子:
<?php $conn = @mysql_connect("localhost","root","root123"); if (!$conn){ die("连接数据库失败:" . mysql_error()); } mysql_select_db("test", $conn); mysql_query("set character set 'gbk'"); $result = mysql_query("SELECT uid,username FROM user"); while($row = mysql_fetch_object($result)){ echo "用户ID:".$row->uid."<br />"; echo "用户名:".$row->username."<br />"; } ?>
浏览器输出:
用户ID:1 用户名:admin 用户ID:2 用户名:小明 用户ID:3 用户名:Jack 用户ID:4 用户名:小王
您可能感兴趣的文章:
- 一个简单的PHP&MYSQL留言板源码
- 小结下MySQL中文乱码,phpmyadmin乱码,php乱码 产生原因及其解决方法
- jQuery结合PHP+MySQL实现二级联动下拉列表[实例]

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

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.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen
