-1

I need one help.I need to display values inside table using PHP and MySQL.I am explaining my code below.

<?php 
$sql=mysql_query("select * from phr_pincode order by id desc");

while($row=mysql_fetch_array($sql)){
    echo 
      '<tr>
       <td></td>
    <td>'.$row['pincode'].'</td>
      <td>'.$row['area'].'</td>
    <td>'.$row['status'].'</td>
     <td>

     <a>
    <input type='button' class='btn btn-xs btn-green' value='Edit'">  
    </a>
    </td>
    <td>
    <a>
    <input type='button' class='btn btn-xs btn-red' value='Remove'>  
    </a>
     </td>
    </tr>';
    }
?>

Here I am getting some syntax errors.I need here all data to display in a loop.I have left first row,it should take serial no (1,2,3....) and for edit button i need to attach index.php with the respective id after ? inside anchor tag.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • **`mysql_*` is deprecated**. You should switch over to `mysqli_*` or PDO prepared statements. – Ben Feb 09 '16 at 11:27
  • Please dont use the `mysql_` database extension, it is deprecated (gone for ever in PHP7) Especially if you are just learning PHP, spend your energies learning the `PDO` or `mysqli_` database extensions, [and here is some help to decide which to use](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – RiggsFolly Feb 09 '16 at 11:27
  • What is the syntax error are you getting? – Edwin Alex Feb 09 '16 at 11:27
  • yes,i am using that part i have not provided. –  Feb 09 '16 at 11:28

3 Answers3

0

Your syntax errors are because you are using single quotes ' inside your echo, which has a delimiter of single quotes too.

Change the delimiter of the echo command to double quotes " like so:

echo "<tr>
          <td></td>
          <td>".$row['pincode']."</td>
          <td>".$row['area']."</td>
          <td>".$row['status']."</td>
          <td>
              <a>
                  <input type='button' class='btn btn-xs btn-green' value='Edit'>  
              </a>
          </td>
          <td>
              <a>
                  <input type='button' class='btn btn-xs btn-red' value='Remove'>  
              </a>
          </td>
    </tr>";
Ben
  • 8,894
  • 7
  • 44
  • 80
0

Try this:

<?php
$sql = mysql_query("select * from phr_pincode order by id desc");
$counter = 0;
while($row=mysql_fetch_array($sql))
{
    echo "<tr>
    <td>".$counter++."</td> // here increase counter
    <td>".$row['pincode']."</td>
    <td>".$row['area']."</td>
    <td>".$row['status']."</td>
    <td>
        <a>
            <input type='button' class='btn btn-xs btn-green' value='Edit'>
        </a>
    </td>
    <td>
        <a>
            <input type='button' class='btn btn-xs btn-red' value='Remove'>
        </a>
    </td>
</tr>";
}
?>
Pathik Vejani
  • 4,263
  • 8
  • 57
  • 98
0
    <?php 
    $sql=mysql_query("select * from phr_pincode order by id desc");

        while($row=mysql_fetch_array($sql)){
    ?>
<tr>
           <td></td>
        <td><?=$row['pincode']?></td>
          <td><?=$row['area']?></td>
        <td><?=$row['status']?></td>
         <td>

         <a>
        <input type='button' class='btn btn-xs btn-green' value='Edit'">  
        </a>
        </td>
        <td>
        <a>
        <input type='button' class='btn btn-xs btn-red' value='Remove'>  
        </a>
         </td>
        </tr>
    <?php
        }
    ?>

Use of (') is might be getting issue when it's come next (') it will automatically close.. so use this practice....

Nikunj
  • 86
  • 11