Code2Design.com

User login

The Layout

Programming

Graphic Design

Resources

Navigation

C2D Projects

Unsystematic Affiliates

Photoshop Lab XeonFX pixel-efx Capital Tutorials 

Change Language

Who's online

There are currently 0 users and 5 guests online.

Ordering Infromation Using MySQL

I have a script I wrote for my forums I am making, which are coming out nicly, I took David's advice and read my PHP book in further depth. But it doesn't seem to mention how to order information strait for the database as you would want to in a forum.

I have something like:

<?php
$topic 
mysql_query("SELECT `name`, `desc`, `poster`, `id` FROM `topics` WHERE `id` = '$_GET[tid]' ORDER BY `id`");
?>

(Much more to the code that's just a segment)

And what it does is since the only 2 topics I have made are 1 and 2 to test with 2 apears on top of number 1 in the topic view. I know it's the ORDER BY `id` that's screwing it up but I cannot figure out how to get it to order from least to greatest, because not it's going from greatest to least desending.

All help is appresiated!


Submitted by Navigator on February 20, 2008 - 3:11pm.
printer friendly version

ORDER BY clause

ORDER BY has another clause called "ASC" and "DESC" which take whatever column you specify (like "id") and alphabetically order it.

<?php
mysql_query
('SELECT * FROM `table` ORDER BY `id` ASC');
?>

Also, don't use double quotes (") when working with strings as it takes longer. Second, make sure to sanitize your sql (mysql_real_escape_string) or you WILL get hacked! Third, don't set mysql_query() to a string - just do something like:

<?php
$query 
'SELECT `name`, `desc`, `poster`, `id` FROM `topics` WHERE `id` = \''mysql_real_escape_string($_GET['tid']). '\' ORDER BY `id` ASC';

//If there is a problem
if(!mysql_query($query)) { die 'MySQL Error:'mysql_error(); }

//on with the code
?>


Thank you!

I'll be sure to use these tips!


Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br> <br /> <h3>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use BBCode tags in the text, URLs will be automatically converted to links
More information about formatting options



Like what you see?

Why not add more? C2D is looking for other Christian Web Masters who would like to help write articles for this site. If you have expericance in FLASH, CSS/HTML, PHP/MySQL, PhotoShop/GIMP, Blender, Javascript, or just General Design - our users would love to hear what you have to say. Contact Us

delicious   digg   reddit   magnoliacom   newsvine   furl   google   yahoo   technorati