I have store the same data in two tables: tableA and tableB. I want to select first from tableA, and if there is no data, I want to used tableB. tableA and tableB hold exactly the same data. I used tableB for archiving. There maybe times when we delete data from tableA but it will still be there in tableB.
$q = $dbh->query("SELECT * FROM tableA WHERE id='1'");
if($q->fetchColumn==NULL){
$q = $dbh->query("SELECT * FROM tableB WHERE id='1'");
}
foreach($q->fetchAll() as $data):
echo $data['name'];
endforeach;
The problem with this code is that it returns from tableB whereas tableA data is there. How to solve this problem? Please help.
TableA
id, name, age
1 john 45
TableB
bid, id, name, age
1 1 old john 45
If there is no data in tableA, I want the query to select from tableB. But if there is data in TableA, I want to select from it(tableA). My code outputss from tableB.
The errors:
fetchColumn()==NULL//does not output anything
fetchColumn(0)==NULL//does not output anything
fetchColumn==NULL//outputs from tableB
fetchColumn==0//outputs from tableB
fetchColumn()==0/does not output anything
Expected output: john
PHP Version 7.1.1