How to correctly understand PHP get display database data function _php Tutorial

Source: Internet
Author: User
In the application

PHP gets the mysql_result of the Display database data function ()

Mixed mysql_result (resource result_set, int row [, mixed field])
Gets the data for a field from the specified row in the 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. $ result = mysql_query ($query);
  6. $ ID = Mysql_result ($result, 0, "id");
  7. $ name = Mysql_result ($result, 0, "name");
  8. Mysql_close ();

Note that the above code is only the field value of the first data in the output result set, and if you want to output all the records, you need to loop the processing.

 
  
  
  1. For ($i0<= mysql_num_rows ($result); $i + + )
  2. {
  3. $ ID mysql_result($result, 0, "id");
  4. $ name mysql_result($result, 0, "name");
  5. echo "Product: $name ($id)";

Note that if the query field name is an alias, the alias is used in Mysql_result.

PHP gets the mysql_fetch_row of the Display database data function ()

Array mysql_fetch_row (Resource result_set)
Gets the entire row from the Result_set and puts the data into the array.
Example (note and the smart fit of list):

 
  
  
  1. $ Query
    Name from product order by name ";
  2. $ result mysql_query($query);

  3. = Mysql_fetch_row ($result)) {
  4. echo "Product: $name ($id)";

PHP gets the mysql_fetch_array of the Display database data function ()

Array mysql_fetch_array (Resource result_set [, int result_type])
Enhanced version of Mysql_fetch_row ().
Gets each row of the Result_set as an associative array or/and a numeric index array.
By default, two arrays are obtained, result_type can be set:
MYSQL_ASSOC: Return associative array, field name = = Field value
Mysql_num: Returns an array of numeric indices.
Mysql_both: Gets the two arrays. Therefore each field can be referenced by index offset, or by field name.

Example:

 
 
  1. $ Query = "SELECT ID,
    Name from product order by name ";
  2. $ result = mysql_query ($query);
  3. While ($row = mysql_fetch_array
    ($result, Mysql_both)) {
  4. $ name = $row [' name '];
  5. //or $ name = $row [1];
  6. $ name = $row [' id '];
  7. //or $ name = $row [0];
  8. echo "Product: $name ($id)";
  9. }

PHP gets the MYSQL_FETCH_ASSOC of the Display database data function ()

Array Mysql_fetch_assoc (Resource result_set)
Equivalent to Mysql_fetch_array ($result, MYSQL_ASSOC)

PHP gets the mysql_fetch_object of the Display database data function ()

Object Mysql_fetch_object (Resource Result_set)
As with the mysql_fetch_array () function, but instead of an array, an object is returned.
Example:

 
  
  
  1. $ Query = "SELECT ID, name
    from product order by name" ;
  2. $ result = mysql_query ($query);
  3. while ($ row = mysql_fetch_object
    ($result)) {
  4. $ name = $row- > name;
  5. $ name = $row- > ID;
  6. echo "Product: $name ($id)";
  7. }

These functions are all summaries of PHP's access to the display database data functions.


http://www.bkjia.com/PHPjc/446138.html www.bkjia.com true http://www.bkjia.com/PHPjc/446138.html techarticle the mysql_result () mixed mysql_result (resource result_set, int row [, mixed field]) from Result_set is used to obtain the display database data function from the specified row Gets the number of a field ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.