多次显示mysqli结果

时间:2017-10-17 05:14:56

标签: php mysqli

我最近问了这个,但它被标记为重复并删除了。 请在标记之前查看我的问题,因为它不一样,我正在努力解决这个问题。

我希望将一个MYSQLI查询的'公司'和'区域'结果回显到我的页面,在php页面正文的不同位置。

只会显示第一个回音。请出示我的错误。

<?php 

$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/Connections/****";
include_once($path);

$dbhandle=mysqli_connect($hostname_Demo, $username_Demo, $password_Demo, $database_Demo);

if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql_RS1="SELECT * from CompanyName where area = 1";
$result=mysqli_query($dbhandle,$sql_RS1);

?>

<head>
<title>TEST</title>
</head>
<body>

<?php
while($row = mysqli_fetch_assoc($result))
{
    echo $row['company'];
}
?>

<?php
while($row = mysqli_fetch_assoc($result))
{
    echo $row['area'];
}
?>

<?php  $dbhandle->close();  ?>

</body>
</html>

4 个答案:

答案 0 :(得分:3)

您可以使用此代码

<?php
    while($row = mysqli_fetch_assoc($result))
    {
    $data_array[] = $row;
    }

    foreach ($data_array as $data) {
        echo $data['company'];
    }
    foreach ($data_array as $data) {
        echo $data['area'];
    }

使用..这样做,您不希望对数据库进行更多查询。

答案 1 :(得分:-1)

您正在获取相同的第二行,这是不正确的。

这样的事情应该有效

<?php 
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/Connections/****";
include_once($path);

$dbhandle=mysqli_connect($hostname_Demo, $username_Demo, $password_Demo, $database_Demo);

if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql_RS1="SELECT * from CompanyName where area = 1";
$result=mysqli_query($dbhandle,$sql_RS1);
$row =array();
while ($x= mysqli_fetch_assoc($result))
      $row[]=$x;
$i=0;
?>
<head>
<title>TEST</title>
</head>
<body>

<?php
   echo $row[i]['company'];
?>

<?php
   echo $row[i]['area'];
   $i++;
?>

答案 2 :(得分:-1)

尝试合并它们看起来它们只是相同的

<?php
while($row = mysqli_fetch_assoc($result))
{
echo  $row['company']." 
".$row['area'];
}
?> 

答案 3 :(得分:-1)

这就是我的结局。它的工作原理我希望它是正确的。感谢大家的帮助。

$sql_RS1="SELECT * from CompanyName where area = 1";
$result=mysqli_query($dbhandle,$sql_RS1);
$row =array();
while ($x= mysqli_fetch_assoc($result))
  $row_RS1=$x;
?>

<?php
$sql_RS2="SELECT * from employees where EmployeeID = 79";
$result=mysqli_query($dbhandle,$sql_RS2);
$row_RS2 =array();
while ($x= mysqli_fetch_assoc($result))
  $row_RS2=$x;
?>

<head>
<title>TEST</title>
</head>
<body>

<p>
<?php  echo $row_RS1['company']; ?>
</p>
<p>
<?php  echo $row_RS1['company']; ?>
</p>
<p>
<?php  echo $row_RS1['area']; ?>
</p>
<p>
<?php  echo $row_RS2['EmployeeID'];  ?>
</p>
<p>
<?php  echo $row_RS2['Surname']; ?>
</p>
<p>
<?php  echo $row_RS2['EmployeeID'];  ?>
</p>
<p>
<?php  $dbhandle->close();  ?>
</p>
</body>