View Full Version : MySQL Reverse Query?
Blender
05-07-2009, 10:31 PM
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?
05-07-2009, 10:33 PM
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
05-07-2009, 10:58 PM
This is what I had:
<?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);
?>This is what I tried:
<?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);
?>
I want to make the query:
1
2
3
4
5
6
Become:
6
5
4
3
2
1
I can't store it into an array for some reason...
Any ideas why?
Blender
05-07-2009, 11:05 PM
Oops! Never mind...
I typed:
$row['title'] = $title[$temp];
$row['description'] = $description[$temp];
$row['creator'] = $creator[$temp];
$row['url'] = $url[$temp];
Instead of:
$title[$temp] = $row['title'];
$description[$temp] = $row['description'];
$creator[$temp] = $row['creator'];
$url[$temp] = $row['url'];
Fixed!
Thanks for making me type it out!
Floor66
05-07-2009, 11:10 PM
Can't you put "ORDER BY `title` DESC" in there?
Blender
05-07-2009, 11:16 PM
It is a forum that I made.
It displayed the oldest topics first, and newest last.
I needed to flip it around.
Floor66
05-07-2009, 11:36 PM
Well then "ORDER BY `post_date` DESC"?
Wizzup?
05-07-2009, 11:54 PM
Just use ORDER BY <ENTITY1> [ENTITY2...] as Floor66 stated. Also, ASC = Ascending Order, Desc = descending order. Each Entity can be ASC or DESC.
Powered by vBulletin® Version 4.2.1 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.