How to correctly understand the PHP function to obtain and display database data_PHP Tutorial

WBOY
Release: 2016-07-15 13:32:39
Original
866 people have browsed it

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><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>
Copy after login

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>
Copy after login
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>
Copy after login

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>
Copy after login

true


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

TechArticle

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!