• Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
logo logo
  • Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
ENEN
  • 简体简体
  • 繁體繁體
Client Area

Php Tip: Use mysql_fetch_array() to fetch a result row as an associative array, a numeric array, or both

December 19, 2023

Php Tip: Use mysql_fetch_array() to fetch a result row as an associative array, a numeric array, or both

When working with PHP and MySQL, it is crucial to have efficient and flexible methods for retrieving data from the database. One such method is the mysql_fetch_array() function, which allows you to fetch a result row as an associative array, a numeric array, or both. This powerful function provides developers with the flexibility to access data in a way that best suits their needs.

Understanding mysql_fetch_array()

The mysql_fetch_array() function is used to fetch a single row from a result set returned by a MySQL query. It takes the result resource as a parameter and returns an array that corresponds to the fetched row. This array can be accessed using either numeric or associative keys, or both.

Here is the basic syntax of the mysql_fetch_array() function:

$row = mysql_fetch_array($result);

Where $result is the result resource returned by a MySQL query.

Fetching a Result Row as an Associative Array

By default, mysql_fetch_array() returns a row as an associative array. This means that the array keys are the column names from the result set, and the array values are the corresponding values from the fetched row.

Here is an example:

$result = mysql_query("SELECT * FROM users");
$row = mysql_fetch_array($result);

echo $row['name']; // Output: John Doe
echo $row['email']; // Output: john@example.com

In this example, the mysql_fetch_array() function fetches a row from the “users” table and stores it in the $row variable. We can then access the values of specific columns using their respective keys.

Fetching a Result Row as a Numeric Array

If you prefer to access the fetched row using numeric keys instead of column names, you can pass the constant MYSQL_NUM as the second parameter to the mysql_fetch_array() function.

Here is an example:

$result = mysql_query("SELECT * FROM users");
$row = mysql_fetch_array($result, MYSQL_NUM);

echo $row[0]; // Output: John Doe
echo $row[1]; // Output: john@example.com

In this example, the mysql_fetch_array() function fetches a row from the “users” table and stores it in the $row variable. We can then access the values of specific columns using their respective numeric keys.

Fetching a Result Row as Both Associative and Numeric Arrays

If you want to access the fetched row using both associative and numeric keys, you can pass the constant MYSQL_BOTH as the second parameter to the mysql_fetch_array() function.

Here is an example:

$result = mysql_query("SELECT * FROM users");
$row = mysql_fetch_array($result, MYSQL_BOTH);

echo $row['name']; // Output: John Doe
echo $row[0]; // Output: John Doe

In this example, the mysql_fetch_array() function fetches a row from the “users” table and stores it in the $row variable. We can then access the values of specific columns using either their respective keys or numeric indexes.

Conclusion

The mysql_fetch_array() function is a versatile tool for fetching result rows from a MySQL query. By using this function, you can retrieve data as an associative array, a numeric array, or both, depending on your specific requirements. This flexibility allows you to work with the fetched data in a way that best suits your needs.

For more information about PHP and MySQL, and to explore our high-performance VPS hosting solutions, visit Server.HK today.

Recent Posts

  • How to Host a Python Flask or Django Application on Hong Kong VPS (2026)
  • How to Set Up WireGuard VPN on a Hong Kong VPS: Step-by-Step Guide 2026
  • Hong Kong VPS vs DigitalOcean: Cost, Performance, and China Routing Compared (2026)
  • VPS Hosting vs Shared Hosting: Why the Upgrade Is Worth It for Asia-Facing Websites
  • Hong Kong VPS vs Google Cloud Asia: Which Delivers Better China Performance in 2026?

Recent Comments

  1. vibramycin injection on How to Choose the Right Hong Kong VPS Plan: A Buyer’s Guide for 2026
  2. allopurinol for gout on CN2 GIA vs BGP vs CN2 GT: What’s the Real Difference for China Connectivity?
  3. antibiotics online purchase on How to Set Up a WordPress Site on a Hong Kong VPS with aaPanel (Step-by-Step 2026)
  4. linezolid cost oral on Top 5 Use Cases for a Hong Kong Dedicated Server in 2026
  5. metoprolol generic on Hong Kong VPS vs Japan VPS: Head-to-Head for Asia-Pacific Deployments in 2026

Knowledge Base

Access detailed guides, tutorials, and resources.

Live Chat

Get instant help 24/7 from our support team.

Send Ticket

Our team typically responds within 10 minutes.

logo
Alipay Cc-paypal Cc-stripe Cc-visa Cc-mastercard Bitcoin
Cloud VPS
  • Hong Kong VPS
  • US VPS
Dedicated Servers
  • Hong Kong Servers
  • US Servers
  • Singapore Servers
  • Japan Servers
More
  • Contact Us
  • Blog
  • Legal
© 2026 Server.HK | Hosting Limited, Hong Kong | Company Registration No. 77008912
Telegram
Telegram @ServerHKBot