search
HomeBackend DevelopmentPHP TutorialHow to correctly understand the PHP function to obtain and display database data_PHP Tutorial

Using

PHP to get and display database data function mysql_result()

mixed mysql_result(resource result_set, int row [,mixed field])
Get the data of a field from the specified row of result_set. Simple but inefficient.

Example:

  1. $ link1 = @mysql_connect("server1",
    "webuser", "password")
  2. or die("Could not connect
    to mysql server!");
  3. @mysql_select_db("company")
    or die("Could not select database !");
  4. $query = "select id, name
    from product order by name"
    ; 🎜> =
  5. mysql_query($query); $id =
  6. mysql_result($result, 0, "id"); $name =
  7. mysql_result($result, 0, "name "); mysql_close(); Note that the above code only outputs the field value of the first piece of data in the result set. If you want Outputting all records requires loop processing. Note that if the query field name is an alias, the alias will be used in mysql_result.
  8. Mysql_fetch_row, a function of PHP to obtain and display database data ()
array mysql_fetch_row(resource result_set)

Get the entire row from result_set and put the data into the array.

Example (note the clever combination with list):
<ol class="dp-xml">
<li class="alt"><span><span>for ($</span><span class="attribute">i</span><span> = </span><span class="attribute-value">0</span><span>; $i </span><span class="tag"><span>= mysql_num_rows($result); $i++)  </span></span></span></li>
<li><span>{  </span></li>
<li class="alt">
<span>$</span><span class="attribute">id</span><span> = </span><span class="attribute-value">mysql_result</span><span>($result, 0, "id");  </span>
</li>
<li>
<span>$</span><span class="attribute">name</span><span> = </span><span class="attribute-value">mysql_result</span><span>($result, 0, "name");  </span>
</li>
<li class="alt"><span>echo "Product: $name ($id)";  </span></li>
<li><span>} </span></li>
</ol>

PHP gets and displays database data function mysql_fetch_array()

array mysql_fetch_array(resource result_set [,int result_type])
Enhancement of mysql_fetch_row() version.
Get each row of result_set as an associative array or/and a numerical index array.
Get two arrays by default, result_type can be set:

MYSQL_ASSOC: Return an associative array, field name => field Value
<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute">query</span><span> = </span><span class="attribute-value">"select id, <br>name from product order by name"</span><span>;   </span></span></li>
<li>
<span>$</span><span class="attribute">result</span><span> = </span><span class="attribute-value">mysql_query</span><span>($query);  </span>
</li>
<li class="alt"><span>while(list($id, $name) <br>= mysql_fetch_row($result)) {  </span></li>
<li><span>echo "Product: $name ($id)";  </span></li>
<li class="alt"><span>} </span></li>
</ol>
MYSQL_NUM: Returns a numerical index array.

MYSQL_BOTH: Gets two arrays. Therefore, each field can be referenced by index offset or by field name.Example:




PHP gets and displays database data function mysql_fetch_assoc()


array mysql_fetch_assoc(resource result_set)

Equivalent to mysql_fetch_array($result, MYSQL_ASSOC)


PHP gets and displays database data function mysql_fetch_object()
<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute">query</span><span> = </span><span class="attribute-value">"select id,<br> name from product order by name"</span><span>;  </span></span></li>
<li>
<span>$</span><span class="attribute">result</span><span> = </span><span class="attribute-value">mysql_query</span><span>($query);  </span>
</li>
<li class="alt">
<span>while($</span><span class="attribute">row</span><span> = </span><span class="attribute-value">mysql_fetch_array<br></span><span>($result, MYSQL_BOTH)) {   </span>
</li>
<li>
<span>$</span><span class="attribute">name</span><span> = $row['name'];</span>
</li>
<li>
<span>//或者 $</span><span class="attribute">name</span><span> = $row[1];  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">name</span><span> = $row['id'];</span>
</li>
<li class="alt">
<span>//或者 $</span><span class="attribute">name</span><span> = $row[0];  </span>
</li>
<li><span>echo "Product: $name ($id)";  </span></li>
<li class="alt"><span>} </span></li>
</ol>

object mysql_fetch_object(resource result_set) It has the same function as mysql_fetch_array(), but it returns not an array, but It is an object.

Example:




The above functions are a summary of PHP's functions for obtaining and displaying database data.




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

www.bkjia.com
<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute">query</span><span> = </span><span class="attribute-value">"select id, name <br>from product order by name"</span><span>;  </span></span></li>
<li>
<span>$</span><span class="attribute">result</span><span> = </span><span class="attribute-value">mysql_query</span><span>($query);   </span>
</li>
<li class="alt">
<span>while($</span><span class="attribute">row</span><span> = </span><span class="attribute-value">mysql_fetch_object<br></span><span>($result)) {  </span>
</li>
<li>
<span>$</span><span class="attribute">name</span><span> = $row-</span><span class="tag">></span><span>name;  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">name</span><span> = $row-</span><span class="tag">></span><span>id;  </span>
</li>
<li><span>echo "Product: $name ($id)";  </span></li>
<li class="alt"><span>} </span></li>
</ol>

true


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

TechArticle

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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

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 Article

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools