How to Use PHP to associate an array Query Result

Source: Internet
Author: User

You may beThe specific operation code is as follows:

 
 
  1. <?php   
  2. $connection = mysql_connect("localhost", "albert", "shhh");   
  3. mysql_select_db("winestore", $connection);   
  4.  
  5. $result = mysql_query("SELECT cust_id, surname,   
  6. firstname FROM customer", $connection);   
  7.  
  8. while ($row = mysql_fetch_array($result))   
  9. {   
  10. echo "ID:t{$row["cust_id"]}n";   
  11. echo "Surnamet{$row["surname"]}n";   
  12. echo "First name:t{$row["firstname"]}nn";   
  13. }   
  14. ?>  

The mysql_fetch_array () function puts a row of the query result into an array and can be referenced in two ways at the same time. For example, cust_id can be referenced in the following two ways at the same time: $ row ["cust_id"] or $ row [0]. Obviously, the former is much more readable than the latter.

In the multi-table join query of PHP joined array query results, if the two columns have the same name, it is best to separate them with aliases:

SELECT winery. name AS wname,
Region. name AS rname,
FROM winery, region
WHERE winery. region_id = region. region_id;


The column names are referenced as $ row ["wname"] and $ row ["rname"].


When the table name and column name are specified, only the column name is referenced:

SELECT winery. region_id
FROM winery

Column Name Reference: $ row ["region_id"].

The reference of the aggregate function is the reference Name:

SELECT count (*)
FROM customer;

Column Name Reference: $ row ["count (*)"].

The above are all the specific operations on the PHP associated array query results.


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.