Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieving mysql info using php

Status
Not open for further replies.

Swytchcraft

Technical User
Dec 12, 2003
12
US
Below is my code:
When I open the page, it pulls my information asceding in order of input, how do I get them to pull in descending order? Mysql sais to put a DESC somewhere, but where?

<?php


include "faq_config.php";

$result = mysql_query("SELECT * FROM faqs WHERE category_id = '$category_id'");



while ($row = mysql_fetch_array($result)) {

if (($answer == "$row[id]") || ($print == "true")) {

print "<tr>
<td class=\"QA\" valign=\"top\">Q <a name=\"$answer\"></a></td>
<td><pre class=\"question\"><a href=\"$PHP_SELF?answer=0&cat_name=$cat_name&category_id=$category_id\">$row[question]</a></pre></td>
</tr>\n";

print "<tr>
<td class=\"QA\" valign=\"top\">A</td>
<td><pre class=\"answer\">$row[answer]</pre>
<br>
<a href=\"$PHP_SELF?answer=0&cat_name=$cat_name&category_id=$category_id#$answer\">&times; close</a><br></td>
</tr>\n";
}


else {

print "<tr>
<td class=\"QA\" valign=\"top\">Q <a name=\"$row[id]\"></td>
<td><pre class=\"question\"><a href=\"$PHP_SELF?answer=$row[id]&cat_name=$cat_name&category_id=$category_id#$row[id]\">$row[question]</a></pre></td>
</tr>\n";
}


}


mysql_close($link);

?>
 
I have read this page, but it seems that its telling me how to query the database directly. How can I add the sort keyword to my php code so that the info will display in descending order?
 
You don't add it to PHP. You tell MySQL to give you the data in the order you choose, through the use of an ORDER BY clause in your SELECT query.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
OK, I follow now. I have went in and each time I change to DESC under the OPerations tab, it changes it, but when I add a new record, it then brings it up at the bottom again. It orders by ID.

5
1
2
3
4

instead of

5
4
3
2
1

I know mysql like I know quantam physics. How do I change the query to give me the data in descending order by ID?
 
Add the appropriate ORDER BY clause to your SELECT query.

The query your posted code uses is:

SELECT * FROM faqs WHERE category_id = '$category_id'

Which, if you wanted the data in an arbitrary order should have read:

SELECT * FROM faqs WHERE category_id = '$category_id' ORDER BY <some MySQL column-name>

if you want the data in descending order on that table column, then use:

SELECT * FROM faqs WHERE category_id = '$category_id' ORDER BY <some MySQL column-name> DESC




Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Ok, I got it. I got confused because you said I should change the way SQL gives it to me so I assumed I made the change in Mysql.

I changed my php code and it works now. Thanx a million.
 
Actually, you ARE making the change in MySQL. The PHP function mysql_query() simply hands the query to MySQL and gets some kind of information back. By changing the query your script hands to MySQL, you're changing the way MySQL returns the data.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top