【发布时间】:2023-03-20 23:10:01
【问题描述】:
大家好,感谢您帮助我... 我同时有 1 个数据库和 3 个查询。每个查询都选择不同的年份并从该列中获取数据。
这是我的代码
$items = array();
// while we still have rows from the db, display them
while ($row = mysql_fetch_array($result1)) {
$items[] = $row;
}
while ($row = mysql_fetch_array($result2)) {
$items[] = $row;
}
while ($row = mysql_fetch_array($result3)) {
$items[] = $row;
}
mysql_close($connect);
?>
<?php foreach($items as $key => $item) : ?>
<?php print "I DONT KNOW WHAT TO DO"; ?>
<?php endforeach; ?>
当我输入时:
<?php echo print_r($keys); ?>
我可以看到我有一个由 0、1 和 2 组成的数组。我很想从 first(1) 或 second(2) 数组中获取特定的 ROW。
我正在努力解决,但我无法...
非常感谢大家!
编辑:
完整代码:
<?php
include ('db.php'); // for db details
include ('functions.php');
$connect = @mysql_connect($host,$username,$password) or die('<p class="error">Unable to connect to the database server at this time.</p>');
if (!$connect) {
die('Could not connect: ' . mysql_error());
}
@mysql_select_db($database,$connect) or die('<p class="error">Unable to connect to the database at this time.</p>');
$query1 = "SELECT * FROM godina2015 WHERE mjesec='8' AND godina='2015'";
$query2 = "SELECT * FROM godina2015 WHERE mjesec='7' AND godina='2015'";
$query3 = "SELECT * FROM godina2015 WHERE mjesec='6' AND godina='2015'";
$result1 = @mysql_query("$query1") or die('<p class="error">There was an unexpected error grabbing news from the database.</p>');
$result2 = @mysql_query("$query2") or die('<p class="error">There was an unexpected error grabbing news from the database.</p>');
$result3 = @mysql_query("$query3") or die('<p class="error">There was an unexpected error grabbing news from the database.</p>');
$items = array();
// while we still have rows from the db, display them
while ($row = mysql_fetch_array($result1)) {
$items[] = $row;
}
while ($row = mysql_fetch_array($result2)) {
$items[] = $row;
}
while ($row = mysql_fetch_array($result3)) {
$items[] = $row;
}
mysql_close($connect);
?>
<?php foreach($items as $key => $item) : ?>
<?php print_r ($item); ?>
<?php endforeach; ?>
编辑#2
<?php
include ('db.php'); // for db details
include ('functions.php');
$connect = mysql_connect($host,$username,$password) or die('<p class="error">Unable to connect to the database server at this time.</p>');
if (!$connect) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db($database,$connect) or die('<p class="error">Unable to connect to the database at this time.</p>');
$query = "SELECT * FROM godina2015 WHERE mjesec IN(8,7,6) AND godina='2015'";
$result = mysql_query("$query") or die('<p class="error">There was an unexpected error grabbing news from the database.</p>');
$items = array();
// while we still have rows from the db, display them
while ($row = mysql_fetch_array($result)) {
$items[] = $row;
}
mysql_close($connect);
?>
<?php foreach($items as $key => $item) : ?>
<?php echo $items[1]['AWAsistCTR2']; ?>
<?php endforeach; ?>
【问题讨论】:
-
如果您在 foreach 循环中执行
print_r($item);会怎样? -
显示
var_dump($items)。您需要准确显示/解释您的数据结构是什么样的。但考虑到您构建数组的方式,您的第一个查询结果将是$items[0]...$items[n],第二个查询结果将是$items[n+1] ... $items[n+m],等等...... -
@VincentG 我得到了所有数组的列表:[0] => 82 [id] => 82 [1] => 8 [mjesec] => 8 [2] => 2015.. ..等等
-
您可能希望首先向我们展示您正在运行的实际查询。谁知道也许你在他们身上做了一些愚蠢的事情