Page 1 of 1
The most stupid problem: $_REQUEST sorting!
Posted: Tue Jun 30, 2009 4:23 am
by simonmlewis
Dear all
Here is a moment when PHP does one thing on one day, but refuses on another.
I want to sort the data in a table from a MySQL query.
To do this I am passing the field name to a $_REQUEST via the URL, like so:
Code: Select all
<a href='{$_SERVER['PHP_SELF']}?page=adminlinks&order=category' style='text-decoration: none; color: #ffffff;'>Category</a>
...or more specifically....
At the top of the page is this:
Code: Select all
$sort = $_REQUEST['order'];
if($sort == NULL) { $sort=urldisplay; }
The IF statement is for when the page first loads, otherwise it errors.
And this is the MySQL statement:
Code: Select all
$result = mysql_query ("SELECT * FROM lincslinks ORDER BY $sort");
This is the exact same code as a page I am running in a very similar way, that does work.
The error given here is basically say it cannot find anything in results.
The code doesn't cater for "no results found" as there will always be something in there.... so no point really.
If anyone can help or has seen this before and knows of a fool proof method, please tell me.
It's always the stupid things can get in our way, isn't it.

Re: The most stupid problem: $_REQUEST sorting!
Posted: Tue Jun 30, 2009 4:32 am
by BornForCode
Especially for you i made this incredible artwork, masterpiece, feel proud

:
I had also added a length verification because you may have it set but nothing inside
Code: Select all
$sort = (isset($_REQUEST['order']) && strlen($_REQUEST['order']))? $_REQUEST['order'] : 'urldisplay';
Put this code instead of
Code: Select all
$sort = $_REQUEST['order'];
if($sort == NULL) { $sort=urldisplay; }
Who said that beer is not good for advanced thinking?
Re: The most stupid problem: $_REQUEST sorting!
Posted: Tue Jun 30, 2009 4:43 am
by simonmlewis
Thanks - firstly, what does that code say in pseudo? As mine collects the text into a variable and then checks if that variable has anything in it. If it doesn't, it assigns something to it.
Secondly, is the MySQL correct, as even with your code, it still fails.
Re: The most stupid problem: $_REQUEST sorting!
Posted: Tue Jun 30, 2009 5:07 am
by BornForCode
My code checks if you have something set in the request, if nothing i set than sets the value with the default one
There are more than 5 years since i've used mysql_query (i love my DAL).
Your query should looks: $result = mysql_query ("SELECT * FROM lincslinks ORDER BY ". $sort);
I also suppose that after running the query you are getting the results, do you? Anyway please implement some error handling to be able to retrieve the error. Something basic is:
Code: Select all
$result = mysql_query('SELECT * FROM lincslinks ORDER BY '. $sort);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
if(mysql_num_rows($result)) {
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
// do something with the row columns
}
}
and post here the error message
Re: The most stupid problem: $_REQUEST sorting!
Posted: Tue Jun 30, 2009 5:20 am
by simonmlewis
Invalid query: Unknown column 'urldisplay' in 'order clause'
Code: Select all
$sort = isset($_REQUEST['order']) ? $_REQUEST['order'] : 'urldisplay';
Code: Select all
$result = mysql_query ('SELECT * FROM lincslinks ORDER BY '. $sort);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
if(mysql_num_rows($result)) {
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<tr style='ba......";
Re: The most stupid problem: $_REQUEST sorting!
Posted: Tue Jun 30, 2009 5:37 am
by BornForCode
Yes because you dont have that column. I suppose that in the table definition there is no urldisplay column.
In this case pick another column for ordering replace the 'urldisplay' with anything else.
Re: The most stupid problem: $_REQUEST sorting!
Posted: Tue Jun 30, 2009 5:41 am
by simonmlewis
I do.
I have: url, urldisplay, content, category.
Hence my confusion!
Re: The most stupid problem: $_REQUEST sorting!
Posted: Tue Jun 30, 2009 5:49 am
by BornForCode
Try to remove $sort and write the column name in clear, is it working?
Re: The most stupid problem: $_REQUEST sorting!
Posted: Tue Jun 30, 2009 5:58 am
by simonmlewis
not if I replace "$sort" for "category" using your code method. It produces a blank page.
Using mine, it does work, yes, by entering category into the sql query.
Re: The most stupid problem: $_REQUEST sorting!
Posted: Tue Jun 30, 2009 6:40 am
by BornForCode
Sorry no idea than

. I tested on my place and everything runs smoothly.
I cannot help you anymore since i do not know your environment.
Re: The most stupid problem: $_REQUEST sorting!
Posted: Tue Jun 30, 2009 7:02 am
by simonmlewis
As I said - it's a stupid problem!
Re: The most stupid problem: $_REQUEST sorting!
Posted: Tue Jun 30, 2009 7:05 am
by BornForCode
If you have something up on a host, pm me some details (a temporary ftp account) and i will fix it, otherwise keep looking (does it sound like Johnny Walker? i need some alcohol).
Re: The most stupid problem: $_REQUEST sorting!
Posted: Tue Jun 30, 2009 7:09 am
by simonmlewis
No idea.
Everything I sent is the correct code, and the same server is running the correctly working query.