M
M
McThinker2019-10-20 10:19:55
PHP
McThinker, 2019-10-20 10:19:55

How to retrieve multiple values ​​from a row from a database?

Good afternoon. There is a database with
country_name and city_name columns.
How to correctly form a query to display not only the name of the city, but also the country?

if(!empty($_POST["keyword"])) {
$query ="SELECT * FROM country WHERE city_name like '" . $_POST["keyword"] . "%' ORDER BY city_name LIMIT 0,5";
$result = $db_handle->runQuery($query);
if(!empty($result)) { ?>
    <ul id="country-list">
    <?php
    foreach($result as $country) {
    ?>
        <li onClick="selectCountry('<?php echo $country["city_name"]; ?>');"><?php echo $country["city_name"]; ?></li>
    <?php } ?>
    </ul>
<?php }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
netyshka, 2019-10-20
@McThinker

SELECT city_name, city_country FROM country WHERE city_name like ........
.........');"><?php echo $country["city_country"]; ?><?php echo $ country["city_name"]; ?>

D
Dimon Martovsky, 2020-01-17
@Brezencat

Why * if you only need two columns?

select CONCAT(country_name, ' ', city_name) as Country_City
from country
where city_name like '%%';

As an option:
select country_name + ' ' + city_name AS Country_City
from country
where city_name like '%%';

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question