How to order a database table using Order By command

Codes here !

Moderators: egami, macek, gesf

Post Reply
New php-forum User
New php-forum User
Posts: 8
Joined: Fri Aug 03, 2018 6:59 am

Fri Aug 03, 2018 7:20 am

I have a database in MyPHPadmin which contains an address book. It has a Primary auto-Index column which gves each record a unique incrementing number.
When I insert new names they are added to the bottom of the table.
When I want to put the addresses into a table on a form I specify the table and send an Order By query to sort the table by surname, which changes the order that I see in the myPHPtable but when I then download it into a table in a form it is in the original order, based on the auto indexed value in the table that are shown on my page.
Can I get the rows from the table in the order shown in the myPHPAdmin table?

User avatar
New php-forum User
New php-forum User
Posts: 55
Joined: Mon Oct 16, 2017 1:06 pm

Fri Aug 03, 2018 7:18 pm

Post your code.
The XY Problem
The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

New php-forum User
New php-forum User
Posts: 8
Joined: Fri Aug 03, 2018 6:59 am

Sat Aug 04, 2018 8:15 am

Thanks for your help.

My code is attached.

The line: echo include('dbconnect.php'); calls code which decides whether I am working on localhost or a website and changes the names of the databse, table and password accordingly.
The Column Rcd is the Primary Index, which is auto-incrementing.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html lang="EN" dir="ltr" xmlns="">
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type" />
<link rel = "stylesheet"
type = "text/css"
href = "Main1.css" />
<h4><a class="navblack" href="index.htm"> Home </a>&nbsp; |&nbsp;
<a class="navblack" href="data_form-01.htm">Data Form </a>&nbsp; |&nbsp;
<a class="navblack" href="change-01.php"> Change </a>&nbsp; |&nbsp;
<a class="navblack" href="delete-01.php"> Remove </a></h4>

echo include('dbconnect.php');

if($DB_Found == 0)
{echo "<h2> No database was found.</h2>";}
{$db_select = mysqli_select_db($db, $DB_NAME);

if (!$db_select) {echo "<h2>Database selection failed: </h2>" . $sql . mysqli_error();}

$result = mysqli_query($db, "SELECT * FROM members_2018");

if (!$result) {die("<h2>Database query failed : </h2>" . mysqli_error($DB_NAME));}

$result = mysqli_query($db, " ORDER BY Surname");

echo "<table width=\"100%\" height=\"20\" border=\10\" align=\"center\" cellpadding=\"1\" cellspacing=\"0\"
rules=\"rows\" id=\"incidents\" style=\"color:#333333;border-collapse:collapse;text-align:center;\">";
echo "<tbody>";
echo "<tr style=\"color:Red;background-color:#FFFF00;\">";
echo "<th scope=\"col\">Rcd</th>";
echo "<th scope=\"col\">Surname</th>";
echo "<th scope=\"col\">First</th>";
echo "<th scope=\"col\">Status</th>";
echo "<th scope=\"col\">Position</th>";
echo "<th scope=\"col\">House</th>";
echo "<th scope=\"col\">No</th>";
echo "<th scope=\"col\">Road</th>";
echo "<th scope=\"col\">Area</th>";
echo "<th scope=\"col\">City</th>";
echo "<th scope=\"col\">County</th>";
echo "<th scope=\"col\">Code</th>";
echo "<th scope=\"col\">Phone</th>";
echo "<th scope=\"col\">Mobile</th>";
echo "<th scope=\"col\">EMail</th>";
echo "<th scope=\"col\">Tag</th>";
echo "</tr>";

while ($col = mysqli_fetch_array($result))
echo "<tr style=\"color:#333333;background-color:#F7F6F3;\">";
echo "<td>" .$col[0] ."</td>";
echo "<td>" .$col[2] ."</td>";
echo "<td>" .$col[1] ."</td>";
echo "<td>" .$col[3] ."</td>";
echo "<td>" .$col[4] ."</td>";
echo "<td>" .$col[5] ."</td>";
echo "<td>" .$col[6] ."</td>";
echo "<td>" .$col[7] ."</td>";
echo "<td>" .$col[8] ."</td>";
echo "<td>" .$col[9] ."</td>";
echo "<td>" .$col[10] ."</td>";
echo "<td>" .$col[11] ."</td>";
echo "<td>" .$col[12] ."</td>";
echo "<td>" .$col[13] ."</td>";
echo "<td>" .$col[14] ."</td>";
echo "<td>" .$col[15] ."</td>";
echo "</tr>";
echo "</tbody>";
echo "</table>";

New php-forum User
New php-forum User
Posts: 8
Joined: Fri Aug 03, 2018 6:59 am

Sun Aug 05, 2018 7:28 am

I will try to explain the problem more carefully.
I have a table in a database stored by MyphpAdmin.
It contains a list of names and addresses.
It has a Primary, auto incrementing column. I want this so that I can operate on a single record from my PHP form, using its ID number.
I can add more rows from a PHP form, but these are appended to the bottom of the table.
When I download the data from the MyphpAdmin table into a table on my PHP form I want them listed in alphabetical order, based on the contents of the Surname column.
When I send the code from the php form "OrderBy Surname "before downloading the table I can see that the table in MYphpAdmin has been sorted by the column Surname.
But the table downloaded into my PHP form is sorted on the Prmary Index, not the Surname.
Is there a way of getting the data from the MYphpAdmin table in the order it has just been sorted?

php-forum Fan User
php-forum Fan User
Posts: 597
Joined: Fri Apr 01, 2016 2:18 am

Sun Aug 05, 2018 10:24 pm

"order by" works per query, the order is not stored. So you have to apply "order by" in the query for your form.

Post Reply