php 从数据库中获取列值并显示在 html table 列中

php fetch column values from database and display in html table column

我在数据库名称 Accounts 中有一个 table,其中我有很多行和很多列,我想在我的 [= 中显示一列 Account(所有值) 16=] table。我尝试了很多方法来显示特定列值而不使用 html 中的索引,使用 php,我正在使用 MySql

$storeArray = Array();
while($rowval = mysql_fetch_array($whole, MYSQL_ASSOC))
{ 
$storeArray[] =  $rowval['Account'];  
$status= $rowval['status'];
$ph1= $rowval['Phone1'];
$ph2= $rowval['Phone2'];
}

通过在 <td> 中使用 <?php echo $storeArray[0]; ?><?php echo $storeArray[1]; ?> 我得到了解决方案。我的问题是有什么办法,它会自动显示所有值而不提供任何 index?

这是一个非常复杂的问题。我认为我能做的最好的就是为你指明正确的方向。在 w3schools.com 上有一个很好的教程。你至少应该阅读这些:

PHP - Connect to MySQL

PHP - Select Data From MySQL

也许

PHP - Limit Data Selections From MySQL

$conn=new mysqli("localhost","root","","your_db");

$rows=$conn->query("select username from User");

echo "<table border='1'>";
echo "<tr><th>Username</th></tr>";
while(list($username)=$rows->fetch_row()){
  echo "<tr><td>$username</td></tr>";
}
echo "</table>";
**file user.php**
<?php
$conn=new mysqli("localhost","root","","your_db");

$rows=$conn->query("select id,username from User");

echo "<table border='1'>";
echo "<tr><th>Username</th></tr>";
while(list($id,$username)=$rows->fetch_row()){
  echo "<tr>";
  echo "<td>";
  echo "<form action='user_details.php' target='_blank' method='post'>";
  echo "<input type='hidden' name='txtId' value='$id' />";
  echo "$id - $username";
  echo "<input type='submit' name='btnView' value='View' />";
  echo "</form>";
  echo "</td>";
  echo "</tr>";
}
echo "</table>";
?>

**file user_details.php**
<?php 

 if(isset($_POST["btnView"])){

   $id=$_POST["txtId"];

   $conn=new mysqli("localhost","root","","your_db");

   $row=$conn->query("select id,username,email,phone from User where id='$id'");

   list($id,$username,$email,$phone)=$row->fetch_row();

   echo $id," ",$username," ",$email," ",$phone;

 }



?>

如果您想在 html table 列中获取多个数据库列。

$conn=new mysqli("localhost","root","","your_db");

$rows=$conn->query("select col1,col2 from Tablename");

echo "<table border='1'>";
echo "<tr><th>col1</th><th>col2</th></tr>";
while(list($col1, $col2)=$rows->fetch_row())
{
  echo "<tr><td>$col1</td><td>$col2</tr>";
}
echo "</table>";