Table of Contents
The difference between PHP functions mysql_fetch_row, assoc, array and object
Home Backend Development PHP Tutorial Differences between PHP functions mysql_fetch_row, assoc, array, and object_PHP tutorial

Differences between PHP functions mysql_fetch_row, assoc, array, and object_PHP tutorial

Jul 13, 2016 am 10:07 AM
array fetch mysql object php function the difference of

The difference between PHP functions mysql_fetch_row, assoc, array and object

1. mysql_fetch_row

This function takes a row from the result set as enumeration data, obtains a row of data from the result set associated with the specified result identifier, and returns it as an array. Each result column is stored in a cell of the array, starting at offset 0.

Note that the offset here starts from 0, which means that the field name cannot be used to obtain the value, only the index can be used to obtain the value. For example:

while($row = mysql_fetch_row($res)){

echo $row['cid'].'>>>'.$row[1].'
';

 }

The value of $row['cid'] here cannot be obtained, but $row[1] can.

2. mysql_fetch_assoc

Get a row from the result set as an associative array, which means that this function cannot use the index to get the value like mysql_fetch_row, but can only use the field name to get the value. For example:

while($row = mysql_fetch_assoc($res)){

echo $row['cid'].'>>>'.$row[1].'
';

 }

Here $row[1] cannot get the value, but $row['cid'] can.

3. mysql_fetch_array

Get a row from the result set as an associative array, or a numeric array, or both. In addition to storing the data in the array as a numeric index, you can also store the data as an associative index, using the field name as the key. name.

That is to say, the result he gets is like an array, and the value can be obtained by key or index. For example:

while($row = mysql_fetch_array($res)){

echo $row['cid'].'>>>'.$row[1].'
';

 }

Here $row['cid'] and $row[1] can get corresponding values.

The functions of mysql_fetch_row and mysql_fetch_assoc add up to mysql_fetch_array.

4. mysql_fetch_object

As the name suggests, get a row from the result set as an object and use the field name as an attribute. So the only way to get the value is:

while($row = mysql_fetch_object($res)){

echo $row->cid.'>>>'.$row->title."
";

 }

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/955270.htmlTechArticleDifferences between PHP functions mysql_fetch_row, assoc, array, and object 1. mysql_fetch_row This function takes a row from the result set as an enumeration Cite data from results associated with the specified result identifier...
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)

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

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.

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.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

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: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

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.

PHP: An Introduction to the Server-Side Scripting Language PHP: An Introduction to the Server-Side Scripting Language Apr 16, 2025 am 12:18 AM

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP vs. Python: Use Cases and Applications PHP vs. Python: Use Cases and Applications Apr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Centos install mysql Centos install mysql Apr 14, 2025 pm 08:09 PM

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

See all articles