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

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

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

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.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

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.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

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 uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.
