Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Php script to sort SQL database names alphabetical

Posted on 2013-02-01
3
Medium Priority
?
465 Views
Last Modified: 2013-02-01
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?
Thank
 i have added the incert script just in case

<?php 

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


$fname=$_POST["vname"]; 


// 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          
        database.<p>"; 
} 
else 
{ 
        do_error("Failed to add record"); 
} 

function  do_error($error) 
{ 
        echo  $error; 
        die; 
} 

?> 

Open in new window

0
Comment
Question by:paddy086
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1600 total points
ID: 38843429
no. the INPUT (INSERT) into the table will not and cannot "sort" the names alphabetically.
you have to do this during the SELECT ...
SELECT ID, NAME FROM teacher ORDER BY NAME

Open in new window

0
 
LVL 20

Assisted Solution

by:Mark Brady
Mark Brady earned 200 total points
ID: 38843462
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.
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
ID: 38843497
You might want to get this book and work through the examples.
http://www.sitepoint.com/books/phpmysql5/

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.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question