I have a query that does not work. I am trying to get data from two tables. The important result is that I can put all the data into the while loop at the end. If I edit the query to one table, all works fine, so I have narrowed it down to the SELECT statement. The tables are innodb with the userid as the foreign key.

Anyone have any suggestions?

Thank you

Gary

PHP Code:
 
    
try{
$sql "SELECT pocontact.recipient_fn, pocontact.recipient_mi, pocontact.recipient_ln FROM pocontact  
 
UNION SELECT 
 gb_messages.msg_date, gb_messages.msg_id FROM gb_messages
 WHERE userid ='
$userid'";
$q $conn->prepare($sql);
$q->execute();
} catch (
PDOException $e) {
    print 
"Error!: " $e->getMessage() . "<br/>";
    die(
'Ooops');
}

$count $q->rowCount();
if(
$count>= 1){
echo 
'<h3>Existing Messages</h3>';
}else{
if(
$count == 0){
echo 
'<h3>You have Not Created any Messages</h3>';
}
}
echo 
'<table>';
while(
$row $q->fetch())
{

echo 
'<tr><td>'$row['recipient_fn']. ' '.$row['recipient_mi'] .' ' $row['recipient_ln'].'</td><td>'$row['relation'].'</td><td>'$row['msg_date'].' </td><td><a href="update-goodbye.php?msg_id='.$row['msg_id'].'">Edit</a></tr>';

}
echo 
'</table>'