简体   繁体   中英

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

Fruit table

 id | name 
 1  | Apple 
 2  | Banana

In fruit_prices there are three columns id | fruit_id | price id | fruit_id | price 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 (ie 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?

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

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;

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.

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.

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.

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

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"

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM