$select_query2 = "SELECT * FROM EmployeeBirthdayDetails Order By E_ID";
                $employeedetails = mysql_query($select_query2);
                $emp_details = array();
                $j =0;
                while($row2 = mysql_fetch_array($employeedetails))
                {
                        $emp_details[$j] = $row2;
                        $j++;
                }

                echo "<table border='1' width='100%'>
                <tr>
                <th>Employee ID</th>
                <th>Employee Name</th>
                <th>Date Of Birth</th>
                <th>Edit</th>
                <th>Delete</th>
                </tr>";

                for($i=0;$i<sizeof($emp_details);$i++)
                {
                    $empId=$emp_details[$i]['E_ID'];
                    echo "<tr>";
                    echo "<td>" . $empId . "</td>";
                    echo "<td>" . $emp_details[$i]['B_E_Name'] . "</td>";
                    echo "<td>" . $emp_details[$i]['B_E_DOB'] . "</td>";
                    echo "<td align='center'>"."<a href='#' onclick='editdetails(".$empId.");'>Edit</a></td>";
                    echo "<td align='center'><a href='#' onclick='deletedetails(".$empId.");'>Delete</a></td>";
                    echo "</tr>";
                }

                echo "</table>"; 

I want to display the table data in ascending order. But its displayed in the order in which i added it to the table.

Dani AI

Generated

As already pointed out, ORDER BY is what controls sort order on the database side (ASC is explicit but ascending is the default). If the output still looks like insertion order, the cause is usually not the lack of ASC but one of these: the query actually isn’t the one being executed, the column is a text field so sorting is lexicographic, the date column is stored as a string, or PHP later reorders the rows. Quick checks: run the same SELECT in the MySQL client or phpMyAdmin; echo the query string and call mysql_error() right after the query; inspect a dumped row (var_dump) to see how E_ID and B_E_DOB are stored.

Common fixes for data-format issues:

  • When numeric IDs are stored as strings (e.g. "1","10","2"), force numeric sort in SQL. Example:
    SELECT * FROM EmployeeBirthdayDetails
    ORDER BY CAST(E_ID AS UNSIGNED);
  • When IDs include a prefix like "E123", extract the digits and sort numerically:
    ORDER BY CAST(SUBSTRING(E_ID, 2) AS UNSIGNED)
  • When dates are stored as strings in a non-ISO format, convert them to DATE for ordering:
    ORDER BY STR_TO_DATE(B_E_DOB, '%m-%d-%Y')

Additional notes and best practice: store identifier columns as INT and birthdates as DATE so ORDER BY behaves naturally and uses indexes for performance. Verify that the PHP code does not re-sort the array after fetching; pushing rows into an array preserves the database order. Also consider migrating from the old mysql* API to mysqli or PDO with prepared statements (mysql* was deprecated and later removed) for security and future compatibility.

Change $select_query2 = "SELECT * FROM EmployeeBirthdayDetails Order By E_ID"; to $select_query2 = "SELECT * FROM EmployeeBirthdayDetails Order By E_ID ASC";

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.