简体   繁体   中英

how to retrieve data from particular rows of every table of mysql in php?

I have three tables as h_basic, h_amenities and h_images with common column u_name. there are two php pages named product-list and product-review. I want to fetch the details of a particular product on product-review page when I click on the button of that product on product-list page.

I have the code that retrieves a single row from a single table, but i want to retrieve particular rows of all three tables and i also want to take u_name as primary not id since id is varying in all the tables.

 <?php $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : "0"; $sql = ("SELECT h_basic.*, h_images.* FROM h_basic JOIN h_images WHERE h_basic.id = h_images.id"); $result = mysql_query("$sql"); while ($row = mysql_fetch_array($result)) { echo $row['h_name']; echo $row['h_state']; ?> <img src="images/<?php echo $row['h_image01'];?>"><img src="images/<?php echo $row['h_image02'];?>"><img src="images/<?php echo $row['h_image03'];?>"><img src="images/<?php echo $row['h_image04'];?>"> <?php }?> 

Table 1:

在此处输入图片说明

Table 2:

在此处输入图片说明

Just use whatever fields do match as the ON condition of a JOIN

SELECT B.*, I.*, A.*
FROM h_basic B
LEFT JOIN h_images I ON B.u_name = I.u_name
LEFT JOIN h_amenities A IN B.u_name = A.u_name

You would need to add a WHERE clause to get a specific name

Notice this will go faster if you have an index on each table for the joining fields

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM