Link to home
Start Free TrialLog in
Avatar of j_powers
j_powers

asked on

desc sort - what am I missing?

ok. I am going nuts trying to figure this out. I have a db that I want to put the items in, but show on the page in a reverse order. They then click on the item and that takes him to the actual content.

So I have read a lot of information on this, and from what I read, this is the way to make it happen. Yet everytime I run this script, I get:

"Unable to select info"

What is wrong with this script?

<?php
$username="name";
$password="pass";
$database="db";

$link = mysql_connect(localhost,$username,$password) or die( "Unable to get SQL");
mysql_select_db($database) or die( "Unable to select database");

$query="SELECT * FROM table ORDER BY index DESC";
$result=mysql_query($lquery, $link) or die( "Unable to select info");
$result2 = mysql_num_rows($result);


while ($i = mysql_fetch_array($result))
{
$field1=mysql_result($result,$i,"field1");
$field2=mysql_result($result,$i,"field2");
print "$field1 - $field2";
}

?>
Avatar of BogoJoker
BogoJoker

Hi j_powers,

$lquery should be $query? Here is your code:
$result=mysql_query($query, $link) or die( "Unable to select info");


Joe P
Avatar of j_powers

ASKER

Actually, the script is set as $lquery. I categorize the strings per the area I am working in. This area is the listing area, or the "l" area. So all strings were actually "$lquery", "$llink", "$lresult", etc....

I took all the "l"s out to make the structure easier. I just forgot to change the ($query, $link).

The problem is still there.
Okay, if you are sure that index is different for each record in the table then i am at a loss.
You could always reverse it with ORDER BY index ASC

Joe P
So this is correct? Maybe I need to add more....

Normally I list things, so on other pages, I have the following code -

$i = 0;
while ($i < $result2)
{
$field1=mysql_result($result,$i,"field1");
$field2=mysql_result($result,$i,"field2");
print "$field1 - $field2";
$i++;
}

Maybe there is another way to do it.....
Lol oh, maybe that is the problem!!! =)

while ($i = mysql_fetch_array($result))
{
  $field1=$result["field1"];
  $field2=$result["field2"]
   print "$field1 - $field2";
}

Joe P
Still:

unable to select info
Avatar of Roonaan
Hi,

I think you cannot sort on "index", unless you have a field called "index" in your table. Personally I think you to change the below line:
SELECT * FROM table ORDER BY index DESC";
And have it say:
SELECT * FROM table ORDER BY field1 DESC, field2 DESC"; //sort reverse on field1, and when field1 is equal for two records, sort by field2.

-r-
Realize that this is a scaled down version of the code I am actually running. Yes. There is an index. There is always an index on any of my tables. It's also what I use when I cross reference tables.

Nonetheless I have tried to use other fields like field 1, but the result is the same. No change in the desc order. bes
Have you tried using mysql_error?

mysql_query($query, $link) or die(mysql_error())

The problem is not with your php, but with the query.

-r-
ok. I did that, and it came back with "Check the SQL statement on line 6 (the Select statement).

I went back to the DB and put in another field called "index2". I then populated it with "1". I went back and changed the SQL to

$query="SELECT * FROM table ORDER BY index2 DESC";

It came back without error, but it also came back without putting any information in the table. The HTML prints out, so it goes through the statement, but it doesn't populate anything.
SOLUTION
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That did it. Everything is listing the way I want!

Thanks.