[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 581
  • Last Modified:

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";
}

?>
0
j_powers
Asked:
j_powers
  • 6
  • 3
  • 3
  • +1
2 Solutions
 
BogoJokerCommented:
Hi j_powers,

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


Joe P
0
 
j_powersAuthor Commented:
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.
0
 
BogoJokerCommented:
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
j_powersAuthor Commented:
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.....
0
 
BogoJokerCommented:
Lol oh, maybe that is the problem!!! =)

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

Joe P
0
 
j_powersAuthor Commented:
Still:

unable to select info
0
 
RoonaanCommented:
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-
0
 
j_powersAuthor Commented:
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
0
 
RoonaanCommented:
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-
0
 
j_powersAuthor Commented:
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.
0
 
RoonaanCommented:
Your code should read something like:

<?php
error_reporting(E_ALL);

//..your connection code here ..

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

$result = mysql_query($query) or die('Unable to select info');

while($row = mysql_fetch_assoc($result)) {
  $field1=$row["field1"];
  $field2=$row["field2"]
  echo htmlspecialchars($field1).' - '.htmlspecialchars($field2);
}

?>

-r-
0
 
LinuxNubbCommented:
This is incorrect:
while ($i = mysql_fetch_array($result))
{
  $field1=$result["field1"];
  $field2=$result["field2"]
   print "$field1 - $field2";
}

Should be:

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

As the great Roonan had stated above.

0
 
j_powersAuthor Commented:
That did it. Everything is listing the way I want!

Thanks.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now