search
HomeBackend DevelopmentPHP TutorialIn-depth understanding of using mysql_fetch_row() to return query results in the form of an array_PHP tutorial

Same as mysql_result(), mysql_fetch_row() can also be used to obtain the query result set. The difference is that the return value of the function is not a string, but an array. The function is defined as follows.

Copy code The code is as follows:

array mysql_fetch_row(int result)

Parameter description is as follows.
result:
The result identifier returned by the function mysql_query() or mysql_db_query() is used to specify the SQL statement type of the data to be obtained.
The function return value is as follows.
Success: An array that contains the current row data information in the query result set. The array subscript ranges from 0 to the number of record attributes −1, the i-th in the array The element value is the value on the i-th attribute of the record.
Failure: false.
The following mysql_fetch_row() example function is the same as the example in 5.5.1.
Copy code The code is as follows:


2                                                                                                       ");
5 mysql_select_db("Books", $connection);
6 //Query data
7 $query="SELECT * FROM Computers ";
8 $query.="WHERE price >= 20";
9 //echo $query."
";
10 $result=mysql_query($query,$connection);
11 //Use mysql_fetch_row() to obtain data, and output
12 while($row=mysql_fetch_row($result))
13 {
14 echo "Book title: ".$row[1]."
";
15 echo "Price:" $ row [2]. "& Lt; br & gt;";
16 echo "Publishing date:". $ Row [3]. "& Lt; br & gt;";
17 echo "
";
18 }
19 ?>


mysql_fetch_row() gets the data information of the current row. After being referenced, it automatically slides to the next row. In this example, the reference to it in line 12 is:


Copy the code The code is as follows:
while($row= mysql_fetch_row($result))


In this loop, each time mysql_fetch_row() obtains the current row data, assigns it to the array $row, and then automatically slides to the next row; after fetching the last row , the function will return false and the loop ends. In this way, all the data in the result set can be taken out row by row and displayed.


Note
The subscripts in the result array returned by mysql_fetch_row() correspond to the values ​​​​on different attributes, and can only be used in subscripts, not in attribute names. Obtaining the value of the attribute can easily cause confusion in practical applications, so it must be used carefully. At the same time, care should be taken not to use out-of-bounds subscripts during use.
The results of the example running correctly are as follows.


Copy code The code is as follows:
Book title: Data Structure
Price: 20
Publication date: 2001 -01-01
Book title: C language
Price: 23
Publication date: 1998-04-04
Book title: PHP entry technology
Price: 22
Publication date: 2005-05-01



http://www.bkjia.com/PHPjc/327332.html

truehttp: //www.bkjia.com/PHPjc/327332.htmlTechArticleSame as mysql_result(), mysql_fetch_row() can also be used to obtain the query result set. The difference lies in the return of the function. The value is not a string, but an array. The function is defined as follows. ...
Statement
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
Optimize PHP Code: Reducing Memory Usage & Execution TimeOptimize PHP Code: Reducing Memory Usage & Execution TimeMay 10, 2025 am 12:04 AM

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

See all articles

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

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft