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

时间:2021-12-28 08:35:50

UPDATE: I used the following query:

更新:我使用以下查询:

$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);

Is it absolutely necessary to use var_dump? Moreover, I get an undefined offset error on using this:

是否绝对有必要使用var_dump?此外,我使用这个时得到一个未定义的偏移错误:

echo $result[0]['Title']

1 个解决方案

#1


1  

Use execute, then fetchAll to return all your results as an array.

使用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);

Your array will be in a structure that looks like this:

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

$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'],
];

This means you could access each element of the array in your panels by calling:

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

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

Or if you want to loop through them and display all at once:

或者,如果您想循环浏览它们并立即显示所有内容:

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

Read more on execute here which shows to fetch data from a query: http://php.net/manual/en/mysqli-stmt.execute.php

阅读更多关于execute here的信息,该信息显示从查询中获取数据:http://php.net/manual/en/mysqli-stmt.execute.php

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

执行先前使用mysqli_prepare()函数准备的查询。

#1


1  

Use execute, then fetchAll to return all your results as an array.

使用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);

Your array will be in a structure that looks like this:

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

$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'],
];

This means you could access each element of the array in your panels by calling:

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

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

Or if you want to loop through them and display all at once:

或者,如果您想循环浏览它们并立即显示所有内容:

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

Read more on execute here which shows to fetch data from a query: http://php.net/manual/en/mysqli-stmt.execute.php

阅读更多关于execute here的信息,该信息显示从查询中获取数据:http://php.net/manual/en/mysqli-stmt.execute.php

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

执行先前使用mysqli_prepare()函数准备的查询。