Ajmal Jamil
Ajmal Jamil

Reputation: 829

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

I have a table in database name Accounts in which i have many rows and many columns, i want to show a column Account (all values) in my html table. I have tried many method to show a specific column values without using index in html using php and I am using MySql.

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

by using <?php echo $storeArray[0]; ?> and <?php echo $storeArray[1]; ?> in <td> i got the solution. My question is there any way, it automatically show all values without providing any index?

Upvotes: 0

Views: 15857

Answers (4)

Ajmal Jamil
Ajmal Jamil

Reputation: 829

if you want to fetch more than one database column in your html table column.

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

Upvotes: 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;

 }



?>

Upvotes: 1

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

Upvotes: 3

Marc van Nieuwenhuijzen
Marc van Nieuwenhuijzen

Reputation: 1657

This is a pretty elaborate question. I think the best I can do is point you in the right direction. There is a good tutorial for this on w3schools.com. You should at least read these:

PHP - Connect to MySQL

PHP - Select Data From MySQL

and maybe

PHP - Limit Data Selections From MySQL

Upvotes: 1

Related Questions