I have two mysql tables namely tableA and tableB.
tableA
+-----------+--------------+---------+
| tableA_id | title| month|
+-----------+--------------+---------+
|1 | malaria| january |
|2 | pneumonia| january |
|3 | HIV| january |
|4 | tuberculosis | january |
|5 | dengue fever | january |
+-----------+--------------+---------+
tableB
+-----------+-----------+--------------------+---------------+-----------------+
| tableB_id | tableA_id | correspondence_ref | date_received | date_dispatched |
+-----------+-----------+--------------------+---------------+-----------------+
| 1 | 1 | KLF007 |2015-10-10| 2015-10-10|
| 2 | 1 | KLF011| 2015-10-15|2015-10-15|
|3 | 1 | KLF012| 2015-10-17| 2015-10-17|
|4 | 3 | KLF050 | 2015-10-17| 2015-10-17|
|5 | 5 | KLF036| 2015-10-17 |2015-10-17|
+-----------+-----------+--------------------+---------------+-----------------+
I have written a mysql query returns records in tableA that have no matching records in tableB. Here is the mysl query:
select * from tableA left outer join tableB on tableA.tableA_id = tableB.tableA_id where tableB.tableA_id is null;
The above statement returns this:
+-----------+--------------+---------+-----------+-----------+--------------------+---------------+-----------------+
| tableA_id | title | month| tableB_id | tableA_id | correspondence_ref | date_received | date_dispatched |
+-----------+--------------+---------+-----------+-----------+--------------------+---------------+-----------------+
| 2 | pneumonia | january |NULL |NULL | NULL| NULL| NULL|
|4 | tuberculosis | january |NULL |NULL | NULL| NULL| NULL|
+-----------+--------------+---------+-----------+-----------+--------------------+---------------+-----------------
My problem is that i want php to echo out tableA_id but this does not work. See the code snippet below
<?php
$con=dbConnect();
$query=select * from tableA left outer join tableB on tableA.tableA_id = tableB.tableA_id where tableB.tableA_id is null;
$sql=$con->prepare(query);
$sql->execute();
$sql->setFetchMode(PDO::FETCH_ASSOC);
while ($row = $sql->fetch()){
$tableA_id = $row ['tableA_id'];
$month = $row ['month'];
echo $tableA_id;
}
?>
What can i be possibly be doing wrong? How comes iam able to echo out month and not tableA_id?