-1

I have 3 columns name,id,amount. I created 5 rows with same name. Now i retrieve one row which is topped. Here i added my code,

<?php
$name = "SELECT DISTINCT name
FROM calculation";
$query = mysqli_query($conn,$name);

$count = mysqli_num_rows($query);
while($row = mysqli_fetch_assoc($query)){

echo "<div class='container'>
        <table class='table'>
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>".$row['name']."</td>
        <td>".$row['phone']."</td>
        <td>".$row['balance']."</td>
      </tr></table></div>";
               }    
                mysqli_close($conn);
                     ?>
Barmar
  • 741,623
  • 53
  • 500
  • 612
Sri
  • 41
  • 6
  • use `MAX` or `LIMIT` in your query!! – Saty Mar 21 '16 at 09:39
  • "I retrieve one row which is topped" - What do you mean? How is the row you want returned determined? – JimmyB Mar 21 '16 at 09:45
  • You want to retrieve only one row or all rows? – Amit Rajput Mar 21 '16 at 09:48
  • Use your phpmyadmin SQL tab (if youre working with it), and try some queries. Like : SELECT min( id ) AS ID FROM test WHERE name = 'value1'; – Dan Costinel Mar 21 '16 at 09:50
  • i want to avoid all rows exect one based on their amount value – Sri Mar 21 '16 at 09:51
  • Your query only selects the `name` column. What do you expect to find in `$row['phone']` and `$row['balance']` when you didn't request those columns? – Barmar Mar 21 '16 at 09:52
  • we have few rows each one differentiate from amount value so i need to take row depend upon least amount value – Sri Mar 21 '16 at 09:53
  • hi i want to find month difference between mysql stored date and current date using php or jquery is there any way to find that – Sri Mar 22 '16 at 08:59

2 Answers2

0

Use GROUP BY or LIMIT like this:

$name = "SELECT name
FROM calculation GROUP BY name";

OR

$name = "SELECT name
FROM calculation LIMIT 1";
Amit Rajput
  • 2,061
  • 1
  • 9
  • 27
0

Use the LIMIT clause in your SQL query:

$name = "SELECT DISTINCT name
FROM calculation
LIMIT 1";
SGR
  • 403
  • 5
  • 16