473,508 Members | 2,363 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic MySQL query is supposed to order by column name (variable), but won't

24 New Member
I am currently working on a project where I need to sort my MYSQL query results by different categories. How ever the code does not seem to be working properly and I spend too much time on it already.

So here is the code:

[PHP]
if (isset($_GET['sortby'])){$sort = $_GET['sortby'];}
else $sort = '';

$query = "SELECT * FROM list WHERE county = '$counties[$x]' order by '$sort' ";
$db->query($query);
[/PHP]

When I view the results I dont the order I want, the order I posted $_GET

how ever when I let the query be lets say "bandwidth"

[PHP] $query = "SELECT * FROM list WHERE county = '$counties[$x]' order by bandwidth ";[/PHP]

it works.

can you help me where am I going wrong.
Sep 4 '07 #1
6 7615
epots9
1,351 Recognized Expert Top Contributor
order by has to be followed by a column name, your $sort isn't a column name.
Sep 4 '07 #2
pbmods
5,821 Recognized Expert Expert
Heya, Irish.

Changed thread title to better describe the problem (did you know that threads whose titles do not follow the Posting Guidelines actually get FEWER responses?).

Try using backticks (`) around $sort instead of quotes (').
Sep 4 '07 #3
traineeirishprogrammer
24 New Member
$sort is actually a variable and could be anything, by any coloumn name I want. But what ever way it reads or parses it, mysql does not seem to be able to get the information from the variable. I tried the backsticks but they dont work I only get an error when I try to fetch the results. using mysql_fetch.

CAN ANYONE HELP ME IT IS NOT WORKING!!

thanks
Sep 4 '07 #4
Atli
5,058 Recognized Expert Expert
The order by clause requires the name of a column that exists in the table. The column name should not be quoted. You can encapsulate the column name in backticks (`), as pbmods suggested.

Note, that depending on your server and how you created the table, the column names may be case-sensitive.
(My tests show that columns created using backticks are case-sensitive, whether they are encapsulated in the SELECT query or not)

If your $sort variable provides a value that fits this description, your query should run fine. If not, it will fail.
Sep 5 '07 #5
traineeirishprogrammer
24 New Member
Thanks I fixed the problem
Sep 5 '07 #6
pbmods
5,821 Recognized Expert Expert
Heya, Irish.

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Sep 5 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
6598
by: Dariusz | last post by:
I have PHP code (below) which reads data from a MySQL format database. The problem I am having is trying to find out when the last ID entry was made. When the script is executed, the $gbID is...
4
13564
by: Rahul Anand | last post by:
Hi all, I am trying to fetch 5 records, randomly picked and in random order from a MySQL table (MySQL version > 3.23). I wrote my SQL Query as SELECT name,id FROM tablename WHERE active =...
0
3510
by: Lenz Grimmer | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, MySQL 4.0.14, a new version of the popular Open Source/Free Software Database, has been released. It is now available in source and binary...
0
3925
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
13
12123
by: Dark Rayden | last post by:
Hi! I recently got a strange problem and I have no idea on the solution. I try to do a ORDER BY statement with a fixed order of values, because my client want's it this way. My approach is...
1
17630
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
1
3358
by: jlee | last post by:
I'm pretty much a newbie on mysql, and I need some help. I am running mysql Ver 12.22 Distrib 4.0.24, for portbld-freebsd5.4 (i386) on a server hosting an active website. The site's developer...
7
3371
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always...
6
38453
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7115
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7489
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5624
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
1547
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
414
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.