Page 1 of 1

Data from SQL in alphabetical order

Posted: 11 Jan 2021, 16:16
by JanMolendijk
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();
?>

Data from SQL in alphabetical order

Posted: 11 Jan 2021, 20:04
by MarPlo
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.

Data from SQL in alphabetical order

Posted: 12 Jan 2021, 14:13
by JanMolendijk
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 ????

Data from SQL in alphabetical order

Posted: 12 Jan 2021, 18:18
by MarPlo
If you apply ORDER BY to a column with letters, it will sort the results alphabetically (ASC or DESC).

Data from SQL in alphabetical order

Posted: 04 Mar 2021, 12:03
by HayatAnsiklopedisi
Me too want to numeric order. Can u help me?

Data from SQL in alphabetical order

Posted: 04 Mar 2021, 14:15
by MarPlo
@HayatAnsiklopedisi , post the SQL query you have, and explain what result you want to get.