Home >Backend Development >PHP Tutorial >How 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:
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><span>= mysql_num_rows($result); $i++) </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:
<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)
<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
<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
TechArticle