繁体   English   中英

PHP从两个表中选择并回显表名

[英]PHP Select from two tables and echo table name

我正在使用此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"]));

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM