0

I have 2 tables person and country. Table person has 3 columns id_person, name_person and id_country. country has 2 columns id_country and name_country.

I want to show "name_person" and "name_country", just the name of column, not record. How can I get this ?

halfer
  • 19,824
  • 17
  • 99
  • 186
Ahmad Budi U
  • 111
  • 1
  • 12

3 Answers3

5

You can access table columns in MySQL's information schema database:

SELECT `COLUMN_NAME` 
FROM `INFORMATION_SCHEMA`.`COLUMNS` 
WHERE `TABLE_NAME` IN ('country', 'person')
    AND `COLUMN_NAME` LIKE 'name_%';
Stefan Pries
  • 1,886
  • 2
  • 20
  • 27
2

This should do it:

$result = $db->query('SELECT p.name_person, c.name_country FROM person AS p LEFT JOIN country as c ON (p.id_country=c.id_country)');
$fields = array_keys($result->fetch(PDO::FETCH_ASSOC));

If you want to get the data an not only the columns names, just remove the array_keys.

$fields = $result->fetch(PDO::FETCH_ASSOC);
cb0
  • 8,415
  • 9
  • 52
  • 80
0

You can use

    $result = $db->query('SELECT person.name_person, country.name_country FROM person LEFT JOIN country ON (person.id_country=country.id_country)');

    print_r(array_keys($result));
Jahid Mahmud
  • 1,136
  • 1
  • 12
  • 32