Answer the question
In order to leave comments, you need to log in
How to display not the id of the element, but the word that is attached to it?
Good day!
More recently, I began to study working with the database and decided to immediately apply knowledge in practice during the course of training. I linked 2 tables but can't figure out how to interpret the id into a word that refers to it. I'll attach a photo and code below.
Base:
imgur base
Code:
<?php $country_name = get_country_by($seria["countries_id"]); ?> (on the page)
(in a separate file where the connection to the database is registered)
function get_country_by($id) {
global $db;
$countryN= $db->prepare('SELECT country_name FROM country WHERE id = :id');
$countryN->execute([':id' => $countries_id]);
foreach ($countryN as $country_name) {
return $country_name;
}
}
The result is the number 1, but not the name of the country.
Do not throw sticks, if nonsense is written above, it is better to throw a link where you can read about it. Thank you!
Answer the question
In order to leave comments, you need to log in
After this line:
... you need to apply one of the methods:
$countryN->execute([':id' => $countries_id]);
$rows = $countryN->fetchAll();
var_dump($rows);
$row = $countryN->fetch();
var_dump($row);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question