PHP从两个表和回声表名称中进行选择



我正在使用此PHP代码使用Union

从MySQL中的2个表中进行选择
$sql="
    SELECT 
    'ticket_updates' as rowTable,
    sequence, 
    ticket_seq, 
    notes as displaydata, 
    datetime as timestamp, 
    updatedby, 
    CONCAT('<strong>Time Start: </strong>',timestart,' - <strong>Time End: </strong>',timeend) as timestartend
    from ticket_updates where ticket_seq = '".$_GET["seq"]."'
    UNION
    SELECT 
    'ticket_changes' as rowTable,
    sequence, 
    ticket_seq, 
    description as displaydata, 
    datetime as timestamp,  
    changed_by as updatedby, 
    blankfield
    from ticket_changes where ticket_seq = '".$_GET["seq"]."' 
    ORDER by timestamp ASC ";
$stmt = $pdo_conn->prepare($sql);
$stmt->execute(array(':ticket_seq' => $_GET["seq"]));
$records = $stmt->fetchAll(PDO::FETCH_ASSOC);
$num_rows = count($records);
foreach ($records as $result2) {
   echo $result2["rowTable"];
}

,但它不回音ticket_updatesticket_changes

我知道会返回行,因为当我在phpmyadmin中运行查询时,它显示行,并且显示ticket_updatesticket_changes

您的SQL中没有任何占位符,无法匹配execute调用中的参数。尝试以下操作:

$sql="
    SELECT 
    'ticket_updates' as rowTable,
    sequence, 
    ticket_seq, 
    notes as displaydata, 
    datetime as timestamp, 
    updatedby, 
    CONCAT('<strong>Time Start: </strong>',timestart,' - <strong>Time End: </strong>',timeend) as timestartend
    from ticket_updates where ticket_seq = :ticket_seq1
    UNION
    SELECT 
    'ticket_changes' as rowTable,
    sequence, 
    ticket_seq, 
    description as displaydata, 
    datetime as timestamp,  
    changed_by as updatedby, 
    blankfield
    from ticket_changes where ticket_seq = :ticket_seq2 
    ORDER by timestamp ASC ";
$stmt = $pdo_conn->prepare($sql);
$stmt->execute(array(':ticket_seq1' => $_GET["seq"], ':ticket_seq12' => $_GET["seq"]));

您需要两个不同的占位符,您不能两次使用相同的占位符。

最新更新