SRL Archive - General

MySQL Reverse Query?


Blender

2009-05-07 18:31:47

Does anyone know how to reverse the output of a MySQL query with PHP?
I tried storing it into an array and flipping, but no luck.

Any ideas?

Wizzup?

2009-05-07 18:33:01

From: xXxBlender3DxXx
Does anyone know how to reverse the output of a MySQL query with PHP?
I tried storing it into an array and flipping, but no luck.

Any ideas?


Can you give an example? You can't just invert any query.

Blender

2009-05-07 18:58:22

This is what I had:

[php]<?php

$dbconnect = mysql_connect("localhost", ":norris:", ":norris:");

mysql_select_db(":norris:", $dbconnect);

$articles = mysql_query("SELECT * FROM Articles");

while ($row = mysql_fetch_array($articles))
{
print('<a class="pagetitle" href="' . $row['url'] . '">' . $row['title'] . ' </a>');
print("\n<br />\n");
print('<div class="creator">' . $row['creator'] . '</div>');
print("\n<br />\n");
print('<div class="description">' . $row['description'] . '</div>');
print("<hr>\n");
}

mysql_close($dbconnect);

?>[/php]This is what I tried:
[php]<?php

$dbconnect = mysql_connect("localhost", ":norris:", ":norris:");

mysql_select_db(":norris:", $dbconnect);

$articles = mysql_query("SELECT * FROM Articles");
$temp = 0;

while ($row = mysql_fetch_array($articles))
{
$row['title'] = $title[$temp];
$row['description'] = $description[$temp];
$row['creator'] = $creator[$temp];
$row['url'] = $url[$temp];

$temp++;
}

$temp = 0;

while ($temp < count($title))
{
print('<a class="pagetitle" href="' . $url[$temp] . '">' . $title[$temp] . ' </a>');
print("\n<br />\n");
print('<div class="creator">' . $creator[$temp] . '</div>');
print("\n<br />\n");
print('<div class="description">' . $description[$temp] . '</div>');
print("<hr>\n");
}

mysql_close($dbconnect);

?>
[/php]

I want to make the query:
[PHP]1
2
3
4
5
6[/PHP]

Become:
[PHP]6
5
4
3
2
1[/PHP]

I can't store it into an array for some reason…
Any ideas why?

Blender

2009-05-07 19:05:03

Oops! Never mind…

I typed:
[PHP] $row['title'] = $title[$temp];
$row['description'] = $description[$temp];
$row['creator'] = $creator[$temp];
$row['url'] = $url[$temp];[/PHP]

Instead of:
[PHP] $title[$temp] = $row['title'];
$description[$temp] = $row['description'];
$creator[$temp] = $row['creator'];
$url[$temp] = $row['url'];[/PHP]

Fixed!
Thanks for making me type it out!

Floor66

2009-05-07 19:10:55

Can't you put "ORDER BY `title` DESC" in there?

Blender

2009-05-07 19:16:33

It is a forum that I made.
It displayed the oldest topics first, and newest last.
I needed to flip it around.

Floor66

2009-05-07 19:36:56

Well then "ORDER BY `post_date` DESC"?

Wizzup?

2009-05-07 19:54:25

Just use ORDER BY <ENTITY1> [ENTITY2…] as Floor66 stated. Also, ASC = Ascending Order, Desc = descending order. Each Entity can be ASC or DESC.