gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:16:09 27 4
gpt4 key购买 nike

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

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

关于PHP 从两个表中选择并回显表名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21437080/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com