i'm having a problem with a code i've written to fetch data from 3 tables, its a tourism website, and i would like the user to search across three tables :

  • hotel [HNo,Name,Description,Location,Address,Price]
  • resturant [RNo,Name,Description,Location,Address,Price]
  • parks [ParkNo,Location,Description,Address]

my select statement is as follows :

SELECT hotel.HNo
      ,hotel.Name
      ,hotel.Description
      ,hotel.Location
      ,hotel.Address
      ,hotel.Price
      ,restaurant.RNo
      ,restaurant.Name
      ,restaurant.Description
      ,restaurant.Location
      ,restaurant.Address
      ,restaurant.Price
      ,parks.ParkNo
      ,parks.Location
      ,parks.Description
      ,parks.Address
FROM hotel, restaurant, parks 
WHERE hotel.Name LIKE '%".$keyword."%' 
   OR parks.Location LIKE '%".$keyword."%'
   OR restaurant.Name LIKE '%".$keyword."%'

Now the problem is, after fetching the data using while($data = mysql_fetch_array($sql)) I'm not able to identify whether i'm printing data from hotel, restaurant or parks table, its necessary since parks don't have names, and user can make a reservation if its a hotel.

Thank you in advance

有帮助吗?

解决方案

You can assign aliases to columns in your SELECT clause to give them distinct names in your result.

E.g.

SELECT hotel.HNo, hotel.Name AS hotelName, hotel.Description AS hotelDesc, hotel.Location AS hotelLocation

and then you refer to these aliases when working with the rows contained in your query result. E.g.

while($data = mysql_fetch_array($sql)) {
   echo $data['hotelName']; 
   ...
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top