0
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<table id="users" border="1">
        <tr>
            <td>ID</td>
            <td>Name</td>
            <td>Age</td>
            <td>Email</td>
            <td>DOB</td>
        </tr>
        <tr>
            <td>1</td>
            <td><input size=25 type="text" id="Name" readonly=true/></td>
            <td><input size=10 type="text" id="Age" readonly=true/></td>
            <td><input size=25 type="text" id="Email" readonly=true/></td>
            <td><input size=10 type="date" id="DOB" readonly=true/></td>
        </tr>
    </table>
<?php
  $con = mysql_connect("127.0.0.1","root","test") or die(mysql_error());
  $db = mysql_select_db("lr") or die(mysql_error());
  $sql=mysql_query("SELECT * FROM user ")or die(mysql_error());
  $count=0;

while($row = mysql_fetch_array($sql))
{
    $name=$row['Name'];
    $age=$row['age'];
    $email=$row['Email'];
    $dob=$row['DOB'];

    echo  '<table id="users" border="">
        <tr>
            <td>$count</td>
            <td>$name</td>
            <td>$age</td>
            <td>$email</td>
            <td>$dob</td>
        </tr>
  </table>  ';  
$count++;   

}

?>
<form id="form1" name="form1" method="post" action="add.php">
<input type="submit" name="Add" id="Add" value="Add" />
<input type="submit" name="Remove" id="Remove" value="Remove" />
<input type="submit" name="Edit" id="Edit" value="Edit" />

</form>

</body>
</html>

this is what i have tried also have retrieved data from mysql database but unable to show it on my html table. the above code does everything except showing values on the table. do check the while loop that print value to the table

3
  • have you print the row value at each time..?? Commented Jun 4, 2013 at 12:56
  • For 1 form 1 submit button is enough, otherwise action will be the same for all of them but different isset() Commented Jun 4, 2013 at 12:58
  • Required comment: mysql_ is being deprecated, you should be using mysqli_ ... but other than that, is it printing empty rows, or is it basically returning zero results? Commented Jun 4, 2013 at 12:59

3 Answers 3

2

Try like this

echo  '<table id="users" border="">';
while($row = mysql_fetch_array($sql))
{
   $name=$row['Name'];
   $age=$row['age'];
   $email=$row['Email'];
   $dob=$row['DOB'];

   echo 
       '<tr>
           <td>'.$count.'</td>
           <td>'.$name.'</td>
           <td>'.$age.'</td>
           <td>'.$email.'</td>
           <td>'.$dob.'</td>
       </tr>';

   $count++;   
}
echo ' </table>  ';

And try to use mysqli_* functions or PDO statements due to the mysql_* functions are deprecated.

Sign up to request clarification or add additional context in comments.

3 Comments

You corrected the problem, but didn't explain what was wrong.
Actually I thought he can compare his and mine that I removed those single quoted values and kept the table header and footer tags outside of while loop so that he can get the one table with multiple rows
it generates output but not in line with the mention table above .. can you please tell how to do that ?
0

Variables inside of single-quoted strings aren't expanded.

From Strings:

Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.

You have to use double-quotes around your string, then single-quotes around your HTML properties inside of the string.

while($row = mysql_fetch_array($sql))
{
    $name=$row['Name'];
    $age=$row['age'];
    $email=$row['Email'];
    $dob=$row['DOB'];

    echo  "<table id='users' border=''>
        <tr>
            <td>$count</td>
            <td>$name</td>
            <td>$age</td>
            <td>$email</td>
            <td>$dob</td>
        </tr>
  </table>";

2 Comments

but its not alligned with the above table... how to allign it neatly in the provided table ?
To make the widths of the columns match, you could put all of the rows in the same table by starting and ending the table tags outside of the while loop, as in Gautam3164's answer, or you could use CSS to define the widths of the fields.
0

Once check $sql query getting the rows by putting print_r($row); in while loop after that do some modifications in while loop

while($row = mysql_fetch_array($sql)){
echo  "<table id='users' border=''>
    <tr>
        <td>".$count."</td>
        <td>".$row['Name']."</td>
        <td>".$row['age']."</td>
        <td>".$row['Email']."</td>
        <td>".$row['DOB']."</td>
   </tr> </table>";  $count++;   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.