473,387 Members | 1,529 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,387 software developers and data experts.

simples way to sort a mysql result

This may be a stupid question, but what is the easiest way to sort a mysql
resultset?

For example I have a resulset containing a colomn 'product_name_english' and
'product_name_french' and want to sort the result on one of these colums
based on a language parameter.

I know how to do this in a query ("ORDER BY") but how can I do this after
the query has been executed?
..soma
Jul 17 '05 #1
5 10203
> This may be a stupid question, but what is the easiest way to sort a
mysql
resultset?

For example I have a resulset containing a colomn 'product_name_english'
and
'product_name_french' and want to sort the result on one of these colums
based on a language parameter.

I know how to do this in a query ("ORDER BY") but how can I do this after
the query has been executed?
.soma


You can use mysql_fetch_array(), then use all the functions related to
arrays
--
Alexandre Lahure
Point 52, Solutions Internet "Ready to Start"
http://www.point52.com/

"Computers are like air conditioners,
They don't work when you open windows"
Jul 17 '05 #2
On Fri, 7 Nov 2003 13:52:35 +0100, "somaBoy MX" <no**@nonesuch.net>
wrote:
This may be a stupid question, but what is the easiest way to sort a mysql
resultset?

For example I have a resulset containing a colomn 'product_name_english' and
'product_name_french' and want to sort the result on one of these colums
based on a language parameter.

I know how to do this in a query ("ORDER BY") but how can I do this after
the query has been executed?


You're always best to get the DBMS to do the sorting if you can, as it
is optimised for that sort of thing (no pun intended :-) :

$sql = "select column from table where condition";

switch ( $lang )
{
case "english":
$orderby = "product_name_english";
break;

case "french":
$orderby = "product_name_french";
break;

default:
$orderby = "product_name_english";
break;
}

$sql .= $sql." order by ".$orderby;

Although from what you have described it sounds like your database
isn't fully normalised. If you decide to add another language later on
you'll need to alter your database schema and update your code in the
relevant places.

Create a product table, a language table and a productlanguage table
that links product and language, e.g.

PRODUCT
pid, price, etc.

LANGUAGE
lid, name
1 English
2 French

PRODUCTLANGUAGE
pid, lid, description
1 1 Widget
1 2 French Widget
2 1 Gizmo
2 2 French Gizmo

and so on.

--
David ( @priz.co.uk )
The Internet Prisoner Database: http://www.priz.co.uk/ipdb/
The Tarbrax Chronicle: http://www.tarbraxchronicle.com/
Jul 17 '05 #3
"David Mackenzie" <me@privacy.net> wrote ...

: [...] Although from what you have described it sounds like your database
: isn't fully normalised. If you decide to add another language later on
: you'll need to alter your database schema and update your code in the
: relevant places.[...]
Thanks for the e x t e n s i v e information, David !

However, I didn't fully normalize the db because it really isn't that big an
application I'm building. It doesn't need "mission-critical solidness" or
anything like that. Also (and mostly), I'm no dba genius. I've been meaning
to look into advanced db design techniques, but you know how it is...

I was really looking for a function to sort the associative array extracted
from a mysql result with mysql_fetch_assoc().

But I went with your first suggestion, that works fine too.

Thanks,
..soma
Jul 17 '05 #4
On Fri, 7 Nov 2003 16:33:49 +0100, "somaBoy MX" <no**@nonesuch.net>
wrote:
"David Mackenzie" <me@privacy.net> wrote ...

: [...] Although from what you have described it sounds like your database
: isn't fully normalised. If you decide to add another language later on
: you'll need to alter your database schema and update your code in the
: relevant places.[...]
Thanks for the e x t e n s i v e information, David !
Ta!
However, I didn't fully normalize the db because it really isn't that big an
application I'm building.
You say that now, but when someone asks for German translations...
It doesn't need "mission-critical solidness" or
anything like that. Also (and mostly), I'm no dba genius. I've been meaning
to look into advanced db design techniques, but you know how it is...
I was taught DB design at Uni, but forgot most of it after the exam.
It wasn't until I'd been involved in the design of a couple that I got
the hang of it again.

I'm no DBA either; I can manage third normal form and simple joins but
I still get confused over left/right inner/outer joins. I always need
to ask for assistance with these.
I was really looking for a function to sort the associative array extracted
from a mysql result with mysql_fetch_assoc().


Look into the functions that mysql offers such as SUM(), MIN(), MAX()
etc (just a glance through them will do) as well as the functions for
formatting dates and numbers, and the string slicing functions, just
so that you are aware of how much can be done by the database.

--
David ( @priz.co.uk )
The Internet Prisoner Database: http://www.priz.co.uk/ipdb/
The Tarbrax Chronicle: http://www.tarbraxchronicle.com/
Jul 17 '05 #5

Thanks for the advice. I think I'll get me that O'Reilly "MySQL cookbook"
after all :))

..soma
(PS: I too often have to look up the syntax for complex query's. Luckily I
usually code in Dreamweaver, which has a neat little built-in SQL reference
cheat panel :))
Jul 17 '05 #6

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

Similar topics

3
by: Paul Brown | last post by:
I have a datebase of events. I have a form that adds to it, and I have a form that removes from it (with checkboxes). And I have a php file that will view all the events, either all of them or...
0
by: mikeswk | last post by:
Hi, all I'm using jsp to connect to mysql 3.23.44 max using connector 3.0.6 (default charset=latin1) I'm trying to put some multi-byte characters (for example big5) together with latin...
2
by: Stefan Mueller | last post by:
With the PHP command 'sort' I can sort an array. However, it doesn't exactly sort the array I'd like to: <html> <body> <?php $my_array = array("AA", "11"); $my_array = array("AA", "22");...
6
by: weetat.yeo | last post by:
Hi all , I have sql statement below , SELECT serial_no,host_name,chasis_model,chasis_flash_size,chasis_dram_size,...
1
by: paulq182 | last post by:
PLEASE HELP ME WITH MY CODE?? import java.sql.*; import java.io.*; class min_filmdb_rel_mysql { public static void main (String args ) throws SQLException, IOException {
221
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
7
by: sarah aziz | last post by:
Hello all i have a problem and i want to solve it I am sorting data from mysql by php and jquery and something isn't right i don't know what here is the code and i need urgent help Jquery...
2
by: mfaisalwarraich | last post by:
Hi Everybody, I am using the following function to get the data from mysql database. i have entered appnum field as text filed in the database which has leading zeroes to this field like if...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.