0

我正在尝试获取数据库中的所有记录并在表格中逐行显示它们。它只会一次又一次地显示同一行。如何使表格的每一行显示数据库中的下一个结果?

<?php
// Formulate query
$logo = "SELECT logo from stores";
$cat = "SELECT cat from stores";
$commission = "SELECT commission from stores";
$link = "SELECT link from stores";
$name = "SELECT name from stores";
// Perform query
$result1 = mysql_query($logo) or die;
$result2 = mysql_query($cat) or die;
$result3 = mysql_query($commission) or die;
$result4 = mysql_query($link) or die;
$result5 = mysql_query($name) or die('Something went wrong');
//////////////////////////////////////
//////////////////////////////////////
do {
//////////////////////////////////////
$rlogo = mysql_fetch_assoc($result1);
$a = implode($rlogo);
//////////////////////////////////////
$rcat = mysql_fetch_assoc($result2);
$b = implode($rcat);
//////////////////////////////////////
$rcommission = mysql_fetch_assoc($result3);
$c = implode($rcommission);
//////////////////////////////////////
$rlink = mysql_fetch_assoc($result4);
$d = implode($rlink);
//////////////////////////////////////
$rname = mysql_fetch_assoc($result5);
$e = implode($rname);
//////////////////////////////////////
$x = $x + 1;
    } while ($x <= 1);
?>
4

1 回答 1

1

如果您增加 $x 但循环在达到 1 时结束......它会立即结束吗?

   $x = $x + 1;
} while ($x <= 1);

通常,人们是这样设置的:

 $query = "select logo, cat, commission, link, name  from stores";

 $result = mysql_query($query); 

 print "<table>";

 // table headers 
 print "<tr><th>logo</th>
            <th>cat</th>
            <th>comission</th>
            <th>link</th>
            <th>name</th></tr>";

 while($row = mysql_fetch_assoc($result))
 {
     print "<tr>"; 

     foreach ($row as $column => $value) 
     {
         print "<td>".$value."</td>";
     }

      // or you can print the table cells like this: 
      //  <td> $row['logo']      </td>
      //  <td> $row['cat']       </td>
      //  <td> $row['commission']</td>
      //  <td> $row['link']      </td>
      //  <td> $row['name']      </td>

     print "</tr>"; 
 }

 print "</table>;

此外,mysql_函数已经过时,很快就会从 PHP 中删除,所以如果你正在学习,你应该学习PDO或者mysqli_改为学习。

于 2013-11-10T21:26:00.463 回答