Php script to sort SQL database names alphabetical

Php script to sort SQL database names alphabetical

Hi I have a table called teachers in it there is two fields ID and Name
I want this name filed to be alphabetical when a user adds a new name. Is there a script I can run that sorts the table from A TO Z or do I need to add something to my input php script to force the name to go alphabetical?
 i have added the incert script just in case


// Read values from form using $_POST (safest) 


// Connect to server 
// Replace username and password by your details  

$db = @mysql_connect("localhost","username","password"); 
if (!$db) 
        do_error("Could not connect to the server"); 

// Connect to the database 
// Note that your database will be called username 

@mysql_select_db("test",$db)or do_error("Could not connect to the database"); 

// Run query 

$sql="INSERT INTO teacher (name) values ('$fname')"; 

if (mysql_query($sql,$db)) 
        echo "The following Teacher"; 
        echo "<br>Record: $fname  has been added to the          
        do_error("Failed to add record"); 

function  do_error($error) 
        echo  $error; 


Open in new window

Who is Participating?
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
no. the INPUT (INSERT) into the table will not and cannot "sort" the names alphabetically.
you have to do this during the SELECT ...

Open in new window

Mark BradyConnect With a Mentor Principal Data EngineerCommented:
If you just want to view the names alpabetically in something like phpmyadmin or netwbeans you could click on the column header and it will sort either ascending or descending. There is no way to have an insert query insert a record alphabetically unfortunately.

There are to many things that it would break. For instance, if an id column was set to auto insert then an alpabetical insert would break that system (the numbers would not be incremental).

You can sort all your result when you do  a select query.
Ray PaseurConnect With a Mentor Commented:
You might want to get this book and work through the examples.

It will help you learn how PHP and Databases work together.  And it has examples using PDO.  That's important because MySQL is being deprecated and removed from PHP very soon.  So don't start a new project with the MySQL extension -- choose one of the other APIs or one of the other data bases.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.