gpt4 book ai didi

php - 如何在网页的不同位置显示查询记录?

转载 作者:行者123 更新时间:2023-11-28 23:39:51 24 4
gpt4 key购买 nike

更新: 我使用了以下查询:

$act1 = $dbh->prepare("(SELECT Title, start_date FROM service_o ORDER BY start_date DESC limit 2) UNION (SELECT Title, Start_Date FROM research_o ORDER BY start_date DESC limit 2) UNION (SELECT Title, Start_Date FROM intern_o ORDER BY start_date DESC limit 2) UNION (SELECT Title, Start_Date FROM participate_o ORDER BY start_date DESC limit 2)");

$act1->execute();
$act1->fetchAll();
$result = $act1->fetchall(PDO::FETCH_ASSOC);

是否绝对有必要使用 var_dump?此外,我在使用这个时得到了一个 undefined offset 错误:

echo $result[0]['Title']

最佳答案

使用execute,然后使用fetchAll 以数组形式返回所有结果。

$act1 = $dbh->prepare("SELECT  Title, start_date FROM table1 ORDER BY start_date DESC limit 2 
UNION
SELECT Title, Start_Date FROM research_o ORDER BY table2 DESC limit 2
UNION
SELECT Title, Start_Date FROM intern_o ORDER BY table3 DESC limit 2
UNION
SELECT Title, Start_Date FROM participate_o ORDER BY table4 DESC limit 2 ");

$act1->execute();
$result = $act1->fetchAll(PDO::FETCH_ASSOC);

var_dump($result);

您的数组将采用如下结构:

$result = [
'0' => [ 'Title' => 'some title', 'Start_Date' => '2009-10-15'],
'1' => [ 'Title' => 'some title', 'Start_Date' => '2009-10-15'],
'2' => [ 'Title' => 'some title', 'Start_Date' => '2009-10-15'],
'3' => [ 'Title' => 'some title', 'Start_Date' => '2009-10-15'],
'4' => [ 'Title' => 'some title', 'Start_Date' => '2009-10-15'],
'5' => [ 'Title' => 'some title', 'Start_Date' => '2009-10-15'],
'6' => [ 'Title' => 'some title', 'Start_Date' => '2009-10-15'],
'7' => [ 'Title' => 'some title', 'Start_Date' => '2009-10-15'],
];

这意味着您可以通过调用访问面板中数组的每个元素:

echo $result[0]['Title'] . ' - ' . $result[0]['Start_Date'];

或者如果你想遍历它们并一次显示所有:

foreach ($result as $row) {
echo '<div class="panel">' . $row['Title'] . ' - ' . $row['Start_Date'] . '</div>';
}

在此处阅读有关execute 的更多信息,它显示了从查询中获取数据:http://php.net/manual/en/mysqli-stmt.execute.php

Executes a query that has been previously prepared using the mysqli_prepare() function.

关于php - 如何在网页的不同位置显示查询记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34713833/

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