WebDevDanno
WebDevDanno

Reputation: 1122

Get column value (name) from a Foreign Key reference in PHP

I've got two databases fruit and fruit-prices (for arguement's sake).

In fruit there are two columns id | name

Fruit table

 id | name 
 1  | Apple 
 2  | Banana

In fruit_prices there are three columns id | fruit_id | price where fruit_id is a FOREIGN KEY reference.

 id | fruit_id | price
 1  |    1     | £2.00
 2  |    2     | £3.00

Now I have a PHP function that will print out a table row and cells with the information from the database but currently if I am printing two fruits out my table looks like this.

 Name | Price 
 1    | £2.00
 2    | £3.00 

PHP Code:

$query = mysqli_query($conn, "SELECT * FROM fruits ");

while($row = mysqli_fetch_assoc($query)) {

    $name = $row['fruit_id']; //for comma separation
    $price = $row['price'];
    echo "<tr>";

        echo "<td>"     .$name.     "</td>" . 
             "<td>"     .$price.    "</td>";

    echo "</tr>";
}

Is there an elegant way I can retrieve the name of the fruit (i.e. 1 = Apple, 2 = Banana). Rather than using the unique ID of each fruit.

So then my table will look like this

Name  | Price
Apple | £2.00

...

I hope this makes sense? I'm new to RD concepts. This is a very simple example and does not reflect my entire project so I'm just wondering if this is achievable?

Upvotes: 2

Views: 9080

Answers (8)

arunjos007
arunjos007

Reputation: 4355

This can be done by joining both the tables.Change your sql query with below one.I think it will solve your problem.

"SELECT fruit.name,fruit_prices.price FROM fruit,fruit_prices WHERE fruit_prices.fruit_id = fruit.id";

Upvotes: 3

Suman
Suman

Reputation: 134

Suggestion : You can have a single table viz fruits with 3 columns id, fruit_name, price.

for your table structure :

SELECT fruits.id, name, price 
FROM fruits, fruit-prices 
WHERE fruit_id=fruits.id;

Upvotes: -1

ChoiBedal
ChoiBedal

Reputation: 111

Well, you need to take a look to SQL Joins

query = mysqli_query($conn, "SELECT FRUIT.name,FRUIT_PRICES.price FROM fruits INNER JOIN FRUIT_PRICES ON FRUIT.id = FRUIT_PRICES.fruit.id");

while($row = mysqli_fetch_assoc($query)) {

$name = $row['name']; //for comma separation
$price = $row['price'];
echo "<tr>";

    echo "<td>"     .$name.     "</td>" . 
         "<td>"     .$price.    "</td>";

echo "</tr>";
}

What I wrote means : "Select FRUIT.Name,FRUIT_PRICES.price in FRUIT AND FRUIT_PRICES, considering that FRUITS.id is related to FRUIT_PRICES.id_fruit"

Upvotes: 0

OTTA
OTTA

Reputation: 1081

You need a join between the tables,

SELECT f.name, fp.price
  FROM fruit f
 INNER JOIN fruit_price fp ON f.id = fp.fruit_id

Upvotes: 0

AJ Allen
AJ Allen

Reputation: 71

You would want to use a join and specify the columns you would like to select.

SELECT f.name, fp.price FROM fruit as f
JOIN fruit-prices as fp ON f.id=fp.fruit_id;

Upvotes: 1

Eternal1
Eternal1

Reputation: 5625

Use of JOIN does what you need.

"SELECT `p`.`price`, `f`.`name` FROM `fruit_prices` `p`
JOIN `fruits` `f` ON `f`.`id` = `p`.`fruit_id`"

Be wary - use of SELECT * here will lead to an error, as both tables have an id field, and the query will break.

Upvotes: 0

KingOfAllTrades
KingOfAllTrades

Reputation: 421

You say they are in two databases, but I think it might just be two tables. If so:

select * from fruit f
left outer join fruit-prices fp
on f.id = fp.fruit_id

The left outer join ensures that if a fruit doesn't have a price it will be returned will null as the price. If you don't want that replace it with an inner join.

Upvotes: 0

Paul T. Rawkeen
Paul T. Rawkeen

Reputation: 4114

It is achievable using JOIN:

SELECT * FROM fruits a JOIN fruits-prices b ON b.fruit_id = a.id

For more friendly column names you can add column aliases and use them further.

Upvotes: 0

Related Questions