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,$u sername,$p assword) 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($lquer y, $link) or die( "Unable to select info");
$result2 = mysql_num_rows($result);
while ($i = mysql_fetch_array($result) )
{
$field1=mysql_result($resu lt,$i,"fie ld1");
$field2=mysql_result($resu lt,$i,"fie ld2");
print "$field1 - $field2";
}
?>
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,$u
mysql_select_db($database)
$query="SELECT * FROM table ORDER BY index DESC";
$result=mysql_query($lquer
$result2 = mysql_num_rows($result);
while ($i = mysql_fetch_array($result)
{
$field1=mysql_result($resu
$field2=mysql_result($resu
print "$field1 - $field2";
}
?>
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.
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
You could always reverse it with ORDER BY index ASC
Joe P
ASKER
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($resu lt,$i,"fie ld1");
$field2=mysql_result($resu lt,$i,"fie ld2");
print "$field1 - $field2";
$i++;
}
Maybe there is another way to do it.....
Normally I list things, so on other pages, I have the following code -
$i = 0;
while ($i < $result2)
{
$field1=mysql_result($resu
$field2=mysql_result($resu
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
while ($i = mysql_fetch_array($result)
{
$field1=$result["field1"];
$field2=$result["field2"]
print "$field1 - $field2";
}
Joe P
ASKER
Still:
unable to select info
unable to select info
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-
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-
ASKER
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
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-
mysql_query($query, $link) or die(mysql_error())
The problem is not with your php, but with the query.
-r-
ASKER
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That did it. Everything is listing the way I want!
Thanks.
Thanks.
$lquery should be $query? Here is your code:
$result=mysql_query($query
Joe P