从mysql表中提取id

时间:2014-03-27 04:28:18

标签: php mysql

抱歉这个问题可能看起来很简单,但我是非常新的PHP,我试图让排名出现在索引页面中,但我不知道如何从列中提取id一张桌子,有两张桌子:

- raking table -
    city = 'Boston'
    ranking_name = 'the best 5 places'
    business_ids = '67,43,1,6,78'

- business table -
    business_id = '67'
    business_name = 'planet pizza'

如何在前面显示如下内容:

<ul>
<h2>the best 5 places</h2>
<li><span>1</span><?php echo $business_name ?></li>
<li><span>2</span><?php echo $business_name ?></li>
<li><span>3</span><?php echo $business_name ?></li>
<li><span>4</span><?php echo $business_name ?></li>
<li><span>5</span><?php echo $business_name ?></li>
</ul>

我已经在桌子上有了id,所以我需要一些非常相似的东西,任何人都可以帮助我。

感谢所有帮助 此致

2 个答案:

答案 0 :(得分:2)

尝试使用此查询

SELECT * FROM business_table WHERE business_id IN ( SELECT business_id FROM ranking_table WHERE city='Boston' );

请参阅Demo

你的代码,

$con=mysqli_connect("HOSTNAEM","USERNAME","PASSWORD","DB") die('Could not connect: ' . mysql_error());

$query = "SELECT * FROM business_table WHERE business_id IN ( SELECT business_id FROM ranking_table WHERE city='Boston' )";

while($row = mysqli_fetch_array($query))
{
  echo '<li><span>1</span>'+$row['$business_name']+'></li></br>';
}

答案 1 :(得分:1)

你可以使用类似......

<?php
$con=mysqli_connect("HOST","USER","PASS","DB");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM business_table WHERE business_id IN ( SELECT business_id FROM ranking_table WHERE city='Boston' )");

while($row = mysqli_fetch_array($result))
  {
  echo '<li><span>1</span>'+$row['$business_name']+'></li>';
  echo "<br>";
  }

mysqli_close($con);
?> 

W3schools.com-Reference