mysqp order by issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cardi777
Forum Commoner
Posts: 54
Joined: Sun Mar 29, 2009 4:26 am

mysqp order by issue

Post by cardi777 »

Hi all. I can't figure out why my code ain't working!?

Code: Select all

$query_table = "SELECT * FROM sys_fields WHERE db_table_name = '$db_table_name' ORDER BY '$order' '$dir'";
What am I doing wrong? The issue is that "order by" isn't doing anything. No errors show. Just lacking functionality.

I used the same code on another table, but it didn't have a "where" statement - it worked fine.

Confused :P

Cheers,
Doug
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: mysqp order by issue

Post by Mark Baker »

cardi777 wrote:What am I doing wrong? The issue is that "order by" isn't doing anything. No errors show either.
Why are you quoting the table name, order by column and direction?

Code: Select all

$query_table = "SELECT * FROM sys_fields WHERE db_table_name = $db_table_name ORDER BY $order $dir";
Print out the value of $query before executing the mysql_query, to be sure that the variables have substituted as you expect.
Trap for errors with or die(mysql_error())
cardi777
Forum Commoner
Posts: 54
Joined: Sun Mar 29, 2009 4:26 am

Re: mysqp order by issue

Post by cardi777 »

There is an or die in place.

Here is my result, first line is an eho of the query. 2nd line is the error which appeared after it:

Code: Select all

SELECT * FROM sys_fields WHERE db_table_name = table_test33 ORDER BY f_pos ASC
Unknown column 'table_test33' in 'where clause'
But if I do this...

Code: Select all

SELECT * FROM sys_fields WHERE db_table_name = 'table_test33' ORDER BY 'f_pos' 'ASC'
.. the data shows, but the ordering isn't functional :P

.. i really don't get it. This column DOES exist. And this query works fine if I take away "ORDER BY f_pos ASC".

Any other suggestions? Really stuck on this one.
cardi777
Forum Commoner
Posts: 54
Joined: Sun Mar 29, 2009 4:26 am

Re: mysqp order by issue

Post by cardi777 »

ahh i figured it out...

this is the query that works.. believe it or not...

Code: Select all

$query_table = "SELECT * FROM sys_fields WHERE db_table_name = '$db_table_name' ORDER BY $order $dir";
Thanks for your help anyway - incase anyone has this same issue, im glad hope this topic will help someone somehow.

Cheers,
Doug
Post Reply