×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Can't sort my table by a particular column, records are returned in autinc order.

Can't sort my table by a particular column, records are returned in autinc order.

Can't sort my table by a particular column, records are returned in autinc order.

(OP)
I am using MySQL Workbench.
I have created the following table.



It contains the folllowing data. They are in alphabetical order except the last two - 50 and 51.



I am attempting to sort via 'name' it via this PHP code:

CODE --> PHP

function DoGenerateAdditionalTradesCheckBoxes()
						{	
							global $g_dbFindATradie;
							$nCount = 0;
							$nNumCols = 20;
							 
							$queryResult = $g_dbFindATradie->query("SELECT id, name, description FROM trades ORDER BY name");
							
							while ($row = $queryResult->fetch_assoc())
						    {
						    	if (($nCount == 0) || (($nCount % $nNumCols) == 0))
						    		echo "<td>";
								echo "<input type=\"checkbox\" id=\"check_" . $row["name"] . "\" name=\"" . $row["name"] . "(" . $row["id"] . ")\" onclick=\"OnClickTradesCheck(this)\" />";
								echo "<label>" . $row["name"] . "</label><br/>";	
					    		$nCount++;
						    	if (($nCount % $nNumCols) == 0)
						    	{
						    		echo "<td>";
						    		$nCount = 0;
						    	}
						    }
						    $queryResult->free_result();
						} 

And this is the result:


So it appears as thought the ORDER BY part of my SQL query is being ignored. What is causing that?

I can't check the query in Workbench because there does not seem to be anyway you view the sorted data output of a query in it.

RE: Can't sort my table by a particular column, records are returned in autinc order.

Yes, your physical table is UNORDERED. The only time you can observe any order is in a query via an ORDER BY clause. But you will not see that in your table.

Don't sweat it!

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein

You Matter...
unless you multiply yourself by the speed of light squared, then...
You Energy!

RE: Can't sort my table by a particular column, records are returned in autinc order.

(OP)
$queryResult = $g_dbFindATradie->query("SELECT id, name, description FROM trades ORDER BY name");

RE: Can't sort my table by a particular column, records are returned in autinc order.

Is it the HTML that orders the rows by id again?

As you ORDER BY the queried result is ordered, but it may be sorted by HTML where you have the id. Look into the CSS, I guess.
Or look into the raw HTML, that should be in the fetched order.

Chriss

RE: Can't sort my table by a particular column, records are returned in autinc order.

You are probably bumping into a conflict with a reserved word ("name"): https://dev.mysql.com/doc/refman/8.0/en/keywords.h...

Employ the backtick: `

Instead of:

CODE --> php

$queryResult = $g_dbFindATradie->query("SELECT id, name, description FROM trades ORDER BY name"); 

Try:

CODE --> php

$queryResult = $g_dbFindATradie->query("SELECT `id`, `name`, `description` FROM `trades` ORDER BY `name`"); 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close