473,378 Members | 1,346 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Displaying database results alphabetically -- Please Help!

I have a php/mysql query working like so:

$Query = "SELECT * FROM $TableName WHERE name LIKE '%".$searchterm."%'
"

All I want to do now is sort them alphabetically. By using the above
current query, the results are listed in the order in which they were
placed in the database.

TIA
Rob
Jul 17 '05 #1
7 2665
Hi Robert!

On 18 Feb 2004 17:25:12 -0800, go****@si.rr.com (Robert) wrote:
I have a php/mysql query working like so:

$Query = "SELECT * FROM $TableName WHERE name LIKE '%".$searchterm."%'
"

All I want to do now is sort them alphabetically. By using the above
current query, the results are listed in the order in which they were
placed in the database.


Check out order by.

BTW.: Is is just incidential that they are returned as they are
stored. Don't bet on it. Entries in a database are not ordered as
such.

--
Jochen Daum - Cabletalk Group Ltd.
PHP DB Edit Toolkit -- PHP scripts for building
database editing interfaces.
http://sourceforge.net/projects/phpdbedittk/
Jul 17 '05 #2
On Wed, 18 Feb 2004 17:25:12 -0800, Robert wrote:
All I want to do now is sort them alphabetically.


ORDER BY

See:
http://www.mysql.com/documentation/m...tml#SQL_Syntax

--
-------------------------
| Jeffrey Silverman |
| jeffrey-AT-jhu-DOT-edu|
-------------------------

Jul 17 '05 #3
Jeffrey Silverman <je*****@jhu.edu> wrote in message news:<pa****************************@jhu.edu>...
On Wed, 18 Feb 2004 17:25:12 -0800, Robert wrote:
All I want to do now is sort them alphabetically.


ORDER BY

See:
http://www.mysql.com/documentation/m...tml#SQL_Syntax


I cannot figure out where to put it in the query. I get errors like:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL
result resource in /var/www/html/finalsite/results.php on line 284
Warning: mysql_free_result(): supplied argument is not a valid MySQL
result resource in /var/www/html/finalsite/results.php on line 319

Where exactly do I put the ORDER BY statement? If i put it where the
manual says, I get the above error. Here are the 2 places I have
tried:

$Query="SELECT * FROM $TableName ORDER BY WHERE nameLIKE
'%".$searchterm."%' "
and
$Query = "SELECT * FROM $TableName WHERE name LIKE '%".$searchterm."%'
ORDER BY name"
Jul 17 '05 #4
On Thu, 19 Feb 2004 13:38:56 -0800, Robert wrote:
I cannot figure out where to put it in the query. I get errors like:


1) Did you *read* the MySQL manual page?
2) Did you *test* your SQL query before you used it?

I realize that a manual can be hard reading for a newbie becasue many
manuals assume a certain amount of previous knowledge. But the answer *is*
in the manual page link I gave you previously. Oh, well, here it is,
explained out...

==============================
SELECT SYNTAX(From the Manual):
==============================

SELECT [STRAIGHT_JOIN]
[SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
[SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS] [HIGH_PRIORITY]
[DISTINCT | DISTINCTROW | ALL]
select_expression,...
[INTO {OUTFILE | DUMPFILE} 'file_name' export_options]
[FROM table_references
[WHERE where_definition]
[GROUP BY {unsigned_integer | col_name | formula} [ASC | DESC], ...
[WITH ROLLUP]]
[HAVING where_definition]
[ORDER BY {unsigned_integer | col_name | formula} [ASC | DESC] ,...]
[LIMIT [offset,] row_count | row_count OFFSET offset]
[PROCEDURE procedure_name(argument_list)]
[FOR UPDATE | LOCK IN SHARE MODE]]
===========================

Now, what does all that mean?

Well, first and foremost, everything in square brackets -- [] - is
*optional*. (So yes, nearly everything is optional!) Secondly, the order
that things appear in this syntax description is the order in which they
must appear in your query. Thirdly, white space is ignored -- but you must
have at least one space between items. It's just that one space is
equivalent to two spaces or a tab. And Fourthly, and most importantly for
you, *immediately* *after* *the* *syntax* is a bunch of *EXAMPLES*.

Example 1:
SELECT 1 + 1;

not useful... skip it.

Example 2:
SELECT CONCAT(last_name,', ',first_name) AS full_name
FROM mytable ORDER BY full_name;

hmmm...This one has an ORDER BY clause in it. Hmmm...I wonder if there
are other examples with ORDER BY clauses?

The rest I leave up to you to figure out.

bye!

--
Jeffrey D. Silverman | jeffrey AT jhu DOT edu
Website | http://www.wse.jhu.edu/newtnotes/

Jul 17 '05 #5
In message <74**************************@posting.google.com >, Robert
<go****@si.rr.com> writes
Jeffrey Silverman <je*****@jhu.edu> wrote in message
news:<pa****************************@jhu.edu>.. .
On Wed, 18 Feb 2004 17:25:12 -0800, Robert wrote:
> All I want to do now is sort them alphabetically.


ORDER BY

See:
http://www.mysql.com/documentation/m...tml#SQL_Syntax


I cannot figure out where to put it in the query. I get errors like:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL
result resource in /var/www/html/finalsite/results.php on line 284
Warning: mysql_free_result(): supplied argument is not a valid MySQL
result resource in /var/www/html/finalsite/results.php on line 319

Where exactly do I put the ORDER BY statement? If i put it where the
manual says, I get the above error. Here are the 2 places I have tried:

$Query="SELECT * FROM $TableName ORDER BY WHERE nameLIKE
'%".$searchterm."%' "
and
$Query = "SELECT * FROM $TableName WHERE name LIKE '%".$searchterm."%'
ORDER BY name"


I suspect the second is almost right (the first is clearly wrong), but
you need to do some error handling which will show you what the contents
of $Query actually was - the chances are there is something small wrong
with it.

Something like:
$Result = mysql_query ($Query)
or die ( $Query . "<p>" . mysql_error() );

This assumes it's going into an HTML page hence the <p> bit. You could
experiment from the DOS box to get the right syntax for the query, then
write the PHP to write the query.

See:

http://www.mysql.com/doc/en/SELECT.html

--
Five Cats
Email to: cats_spam at uk2 dot net
Jul 17 '05 #6
I noticed that Message-ID: <pa****************************@jhu.edu> from
Jeffrey Silverman contained the following:
Example 2:
SELECT CONCAT(last_name,', ',first_name) AS full_name
FROM mytable ORDER BY full_name;

hmmm...This one has an ORDER BY clause in it. Hmmm...


With the same syntax as one of his failed attempts...

$Query = "SELECT * FROM $TableName
^ is this supposed to be here?
WHERE name LIKE '%".$searchterm."%'ORDER BY name";
^ is name one of your fields?
LIKE '%".$searchterm."%'ORDER BY name";
^ add a space

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #7
On Fri, 20 Feb 2004 01:23:19 +0000, Geoff Berrow wrote:
I noticed that Message-ID: <pa****************************@jhu.edu> from
Jeffrey Silverman contained the following:
Example 2:
SELECT CONCAT(last_name,', ',first_name) AS full_name
FROM mytable ORDER BY full_name;

hmmm...This one has an ORDER BY clause in it. Hmmm...


With the same syntax as one of his failed attempts...

$Query = "SELECT * FROM $TableName
^ is this supposed to be here?
WHERE name LIKE '%".$searchterm."%'ORDER BY name";
^ is name one of your fields?
LIKE '%".$searchterm."%'ORDER BY name";
^ add a space


Hmm.

Well, the OP really oughtta test the SQL queries first.

Hear that, OP? Use a MySQL client and run the SQL queries first, get them
working, then cut and paste 'em into your PHP.

Later...

--
-------------------------
| Jeffrey Silverman |
| jeffrey-AT-jhu-DOT-edu|
-------------------------

Jul 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sushmita | last post by:
Hi, I have written an ASP script that connects to a database and runs a stored procedure and displays the results in text format ...using the <TABLE> tag. But I would like to have these results...
4
by: donald | last post by:
Hi all, I have a website running asp (about to move to asp.net soon though) which has a list of DVD's I have the various pages I want, last 10, listing, full listing ect, but the one page i can't...
1
by: Mary | last post by:
Hello, is it possible to insert new data into a datbase alphabetically? For example when a user enters a new row of data, I want the row inserted in the correct order. I do not think this is...
5
by: George | last post by:
Hi, Anyone has the background for explaining? I have made a search on my name and I have got a link to another search engine. The link's title was the search phrase for the other search engine...
1
by: Ruth | last post by:
Alison, You are going to find this a real coincidence! To begin with, I am Roberta Laird's husband. I think you know Roberta. We also are friends with Laura McConnell. Anyway, I am in business...
4
by: Jon | last post by:
Hi, suspect there might not be a good answer to this one. Using vb.net I have a page like this some text datagrid repeater Now the repeater is populated from a simple query that takes no time...
5
by: Brad Baker | last post by:
I'm completely new to ASP.NET programming, so please accept my apologies in advance for asking what is probably an obvious question. :-) I'm trying to write a page which will display the...
1
by: gubbachchi | last post by:
Hi, For my project I have a "add" button, upon clicking it, it will take the user to next page where there will be a text box and list of data displayed below it which is fetched from mysql...
2
by: chazzy69 | last post by:
I having trouble displaying information i have retrieved from an mysql database, i succesfully execute the SELECT query as shown below- $sql = "SELECT PCODE,LOCALITY,STATE FROM `postcode` WHERE...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.