Data from SQL in alphabetical order

Discuss coding issues, and scripts related to PHP and MySQL.
User avatar
JanMolendijk
Posts: 282
Location: Holland Rotterdam

Data from SQL in alphabetical order

Plessant Coursesweb,

I have an grouplist in my database from country`s.

First time I did not do any search-study for this questionm but ask you straitly....

I would like to have my list in alphabetical order.
Can this be easely done into this code ?

Code: Select all

<?php
// connect to the "tests" database
$conn = new mysqli('127.0.0.1', 'comments', '123456', 'comments');
// check connection
if (mysqli_connect_errno()) {
  exit('Connect failed: '. mysqli_connect_error());
}
// SELECT sql query
$sql = "SELECT * FROM `w3_post` where group_id='40'  ORDER BY  `w3_post`.`post_id`  DESC"; 
// perform the query and store the result
$result = $conn->query($sql);
// if the $result contains at least one row
if ($result->num_rows > 0) {
  // output data of each row from $result
  while($row = $result->fetch_assoc()) {
    echo '

 '. $row['title']. ' <br><br> ' ;
  }
}
else {
  echo ' okkok';
}
$conn->close();
?>

MarPlo Posts: 186
Try this sql query:

Code: Select all

$sql = "SELECT * FROM `w3_post` where group_id='40'  ORDER BY `w3_post`.`country` ASC"; 
- 'country' is the column you want in alphabetical order.

JanMolendijk Posts: 282
Is it because ASC it sets on alpha ????

I always thought ASC only do the numberal-thig
Because I just did this

Code: Select all

SELECT * FROM `w3_post` where group_id='40'  ORDER BY  `w3_post`.`title` ASC
& it worked please some explain ????

MarPlo Posts: 186
If you apply ORDER BY to a column with letters, it will sort the results alphabetically (ASC or DESC).

HayatAnsiklopedisi Posts: 1
Me too want to numeric order. Can u help me?

MarPlo Posts: 186
@HayatAnsiklopedisi , post the SQL query you have, and explain what result you want to get.