PHP: Hypertext Preprocessor (original) (raw)
mysqli_result::$field_count
mysqli_num_fields
(PHP 5, PHP 7, PHP 8)
mysqli_result::$field_count -- mysqli_num_fields — Gets the number of fields in the result set
Description
Object-oriented style
Procedural style
Returns the number of fields in the result set.
Return Values
An int representing the number of fields.
Examples
Example #1 Object-oriented style
`<?php
mysqli_report
(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); mysqli=newmysqli("localhost","myuser","mypassword","world");mysqli = new mysqli("localhost", "my_user", "my_password", "world");mysqli=newmysqli("localhost","myuser","mypassword","world");result = mysqli−>query("SELECTName,CountryCode,District,PopulationFROMCityORDERBYIDLIMIT1");/∗Getthenumberoffieldsintheresultset∗/<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>i</mi><mi>e</mi><mi>l</mi><msub><mi>d</mi><mi>c</mi></msub><mi>n</mi><mi>t</mi><mo>=</mo></mrow><annotationencoding="application/x−tex">fieldcnt=</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.8889em;vertical−align:−0.1944em;"></span><spanclass="mordmathnormal"style="margin−right:0.10764em;">f</span><spanclass="mordmathnormal">i</span><spanclass="mordmathnormal">e</span><spanclass="mordmathnormal"style="margin−right:0.01968em;">l</span><spanclass="mord"><spanclass="mordmathnormal">d</span><spanclass="msupsub"><spanclass="vlist−tvlist−t2"><spanclass="vlist−r"><spanclass="vlist"style="height:0.1514em;"><spanstyle="top:−2.55em;margin−left:0em;margin−right:0.05em;"><spanclass="pstrut"style="height:2.7em;"></span><spanclass="sizingreset−size6size3mtight"><spanclass="mordmathnormalmtight">c</span></span></span></span><spanclass="vlist−s"></span></span><spanclass="vlist−r"><spanclass="vlist"style="height:0.15em;"><span></span></span></span></span></span></span><spanclass="mordmathnormal">n</span><spanclass="mordmathnormal">t</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span></span></span></span>result−>fieldcount;printf("Resultsethasmysqli->query("SELECT Name, CountryCode, District, Population FROM City ORDER BY ID LIMIT 1");/* Get the number of fields in the result set */ <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>i</mi><mi>e</mi><mi>l</mi><msub><mi>d</mi><mi>c</mi></msub><mi>n</mi><mi>t</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">field_cnt = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">i</span><span class="mord mathnormal">e</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord"><span class="mord mathnormal">d</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">c</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">n</span><span class="mord mathnormal">t</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>result->field_count;printf("Result set has %d fields.\n", mysqli−>query("SELECTName,CountryCode,District,PopulationFROMCityORDERBYIDLIMIT1");/∗Getthenumberoffieldsintheresultset∗/<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>i</mi><mi>e</mi><mi>l</mi><msub><mi>d</mi><mi>c</mi></msub><mi>n</mi><mi>t</mi><mo>=</mo></mrow><annotationencoding="application/x−tex">fieldcnt=</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.8889em;vertical−align:−0.1944em;"></span><spanclass="mordmathnormal"style="margin−right:0.10764em;">f</span><spanclass="mordmathnormal">i</span><spanclass="mordmathnormal">e</span><spanclass="mordmathnormal"style="margin−right:0.01968em;">l</span><spanclass="mord"><spanclass="mordmathnormal">d</span><spanclass="msupsub"><spanclass="vlist−tvlist−t2"><spanclass="vlist−r"><spanclass="vlist"style="height:0.1514em;"><spanstyle="top:−2.55em;margin−left:0em;margin−right:0.05em;"><spanclass="pstrut"style="height:2.7em;"></span><spanclass="sizingreset−size6size3mtight"><spanclass="mordmathnormalmtight">c</span></span></span></span><spanclass="vlist−s"></span></span><spanclass="vlist−r"><spanclass="vlist"style="height:0.15em;"><span></span></span></span></span></span></span><spanclass="mordmathnormal">n</span><spanclass="mordmathnormal">t</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span></span></span></span>result−>fieldcount;printf("Resultsethasfield_cnt);`
Example #2 Procedural style
`<?php
mysqli_report
(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); link=mysqliconnect("localhost","myuser","mypassword","world");link = mysqli_connect("localhost", "my_user", "my_password", "world");link=mysqliconnect("localhost","myuser","mypassword","world");result = mysqli_query($link, "SELECT Name, CountryCode, District, Population FROM City ORDER BY ID LIMIT 1");/* Get the number of fields in the result set */ fieldcnt=mysqlinumfields(field_cnt = mysqli_num_fields(fieldcnt=mysqlinumfields(result);printf("Result set has %d fields.\n", $field_cnt);`
The above examples will output:
Found A Problem?
There are no user contributed notes for this page.