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

时间:2015-01-07 08:19:26

标签: php mysql sql html5

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

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

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

4 个答案:

答案 0 :(得分:4)

$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>";

答案 1 :(得分:1)

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

PHP - Connect to MySQL

PHP - Select Data From MySQL

也许

PHP - Limit Data Selections From MySQL

答案 2 :(得分:1)

**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;

 }



?>

答案 3 :(得分:1)

如果要在html表列中获取多个数据库列。

$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>";