我是 PHP 新手。我有一个代码,其中我使用 2 个 sql 命令。第一个命令获取第一个最新行,第二个命令获取第二个最新行。此代码位于文件 sqlquery.php
这里是sqlquery.php
<?php
include ("connection.php");
我的代码的问题是我的数组在所有行中打印相同的记录。但是在 Db 中有不同的记录。我的代码只打印每行的第一条记录
我想我的代码输出是这样的
最佳答案
问题是双循环,现在对于第一个查询的每个结果,你为第二个查询的每个结果添加一个数组项,有效地复制数组项,你可以改变这个
while($row1 = mysql_fetch_assoc($result1)){
while($row2 = mysql_fetch_assoc($result2)){
收件人:
while($row1 = mysql_fetch_assoc($result1) && $row2 = mysql_fetch_assoc($result2))
{
最好更改您的 sql 查询,将所有值合并到一个请求中。
你可以做的另一件事,虽然不太好:
while($row1 = mysql_fetch_assoc($result1))
{
$opinion[]= $row1['opinion'];
$action[]= $row1['atitle'];
$long_term[]= $row1['ltitle'];
$outlook[]= $row1['otitle'];
$rating_type[]= $row1['ttitle'];
$short_term[]= $row1['stitle'];
}
while($row2 = mysql_fetch_assoc($result2))
{
$p_long_term[]= $row2['ltitle'];
$p_short_term[]= $row2['stitle'];
}
关于php - 阵列运行不正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32627790/