我有一张包含供应商详情的表格:
id |name |website |phone |image |related to id|
1 |mike |www.example.com |212-555-555 |img/1.jpg| 2 |
2 |bill |www.example.net |212-666-666 |img/2.jpg| 1 |
我的疑问:
$query = mysql_real_escape_string($query);
$raw_results = mysql_query("SELECT * FROM supplier
WHERE (`id` LIKE '".$query."')") or die(mysql_error());
if(mysql_num_rows($raw_results) > 0){
while($results = mysql_fetch_array($raw_results)){
echo $results['name'] ...['phone'] // just an example
}
所以,如果q = 1,我将获得迈克的详细信息。
但如果我想显示相关的ID细节怎么办?
例如:如果账单与迈克有关,那么当q = 1时如何在页面上显示账单的详细信息?
*我知道这不是最好的做法,只是为了证明我的问题
答案 0 :(得分:0)
你可以尝试内连接
$raw_results = mysql_query("SELECT a.name as Member,b.name as RelatedMember
FROM suppliers a
INNER JOIN suppliers b ON a.id= b.related_to_id WHERE (`a`.`id` = '".$id."')
ORDER BY Member");