user3466947
user3466947

Reputation: 1467

extracting id's from a mysql table

sorry for this question it may seem very simple but I'm very new in php, I'm trying to make a ranking appear in the index page, but I don't know how to extract the id's from a column in a table, there's 2 tables:

- 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'

How do I display in the front something like this:

<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>

I already have the id's in the table so I need something very similar, can anyone pls help me.

Appreciate all the help Regards

Upvotes: 0

Views: 81

Answers (2)

i&#39;m PosSible
i&#39;m PosSible

Reputation: 1393

try using this query

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

see Demo

your code,

$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>';
}

Upvotes: 2

Ashish Ratan
Ashish Ratan

Reputation: 2870

You could use something like....

<?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

Upvotes: 1

Related Questions