Can someone help me with PHP MySQL query order?

Asked By 30 points N/A Posted on -

Hello.  I have a MySQL database with 2 columns that I want to sort alphabetically.  Can someone teach me or lend me a code or script in PHP MySQL query order?  Any help would be much appreciated.  Thanks in advance.

Answered By 0 points N/A #139780

Can someone help me with PHP MySQL query order?



Hi Dorothy Gallardo,
To sort alphabetically by PHP:
1- You should connect to DataBase
2- Do a query like this
 a- the query composes by:
      FROM: and choose the table
      WHERE: here use a comparison (id = ‘1’);
      ORDER BY: here to use the order (you should choose the column and order type)
      LIMIT: here number of rows
b- Now compose your query:
• by MySQL library
$sql_txt = “select name from user ORDER BY name”; 
$Query = mysql_query($sql_txt);
And complete your script by use the $Query variable
• by PDO
$sql_txt = “select name, color from user ORDER BY name”;
foreach  ($conn->query($sql) as $row) {
        print $row['name'] . "t";
        print  $row['color'] . "t";
and $conn is the PDO object
if you like to sort by default, you can use this ASC after ORDER BY name 
if you like to sort by reverse,  you can use this DESC after ORDER BY name
and finally to sort use remember "ORDER BY (sort) (order)"

Login/Register to Answer

Related Questions