Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL PHP MySQL
I need you to please help me

I tried several ways and I get error


sort want as follows:
rate DESC
age ASC
Height DESC
weight ASC




also would like to sort by fields


if you could explain with an example would be perfect for noticing how and where exactly is the order by


<? Php
 
$ O ='';
/ / Put the appropriate information:
$ Data = array ('localhost', 'user', 'password');
$ Con = mysql_connect ($ data [0], $ data [1], $ data [2]);
 
if ($ con) {
$ O = 'Error: Could not connect to server. '. mysql_error ();
echo $ o;
exit;
}
 
/ / Change the name of the database for yours
$ Db_name = 'database';
 
if (mysql_select_db ($ db_name, $ con)) {
$ O = 'Error: Could not select database "'. $ Db_name. '". '. mysql_error ();
echo $ o;
exit;
}
 
$ Table = 'users' / / Change this only if you know what it does.
$ Query = "SELECT * FROM $ table";
$ Where = "WHERE";
$ And = 0;
 

if (isset ($ _GET ['name']) &&! empty ($ _GET ['name'])) {
$ Where. = "Name LIKE '% $ _GET [name]%'";
$ And = 1;
}
 
if (isset ($ _GET ['rate'])) {
$ E = explode ('-', $ _GET ['rate']);
 
if (is_numeric ($ e [0]) && is_numeric ($ e [1])) {
if ($ and === 1)
$ Where. = "AND";
 

$ Where. = "Rate BETWEEN $ e [0] AND $ e [1]";
$ And = 1;
}
}
 
if (isset ($ _GET ['age'])) {
$ E = explode ('-', $ _GET ['age']);
 
if (is_numeric ($ e [0]) && is_numeric ($ e [1])) {
if ($ and === 1)
$ Where. = "AND";
 

$ Where. = "Age BETWEEN $ e [0] AND $ e [1]";
$ And = 1;
}
}
 
if (isset ($ _GET ['Height'])) {
$ E = explode ('-', $ _GET ['Height']);
 
if (is_numeric ($ e [0]) && is_numeric ($ e [1])) {
if ($ and === 1)
$ Where. = "AND";
 

$ Where. = "Height BETWEEN $ e [0] AND $ e [1]";
$ And = 1;
}
}
 
if (isset ($ _GET ['weight'])) {
$ E = explode ('-', $ _GET ['weight']);
 
if (is_numeric ($ e [0]) && is_numeric ($ e [1])) {
if ($ and === 1)
$ Where. = "AND";
 

$ Where. = "Weight BETWEEN $ e [0] AND $ e [1]";
$ And = 1;
}
}
 
if (isset ($ _GET ['City']) &&! empty ($ _GET ['city'])) {
if ($ and === 1)
$ Where. = "AND";
 
$ Where. = "City = '$ _GET [City]'";
$ And = 1;
}
 
if (isset ($ _GET ['Eyes']) &&! empty ($ _GET ['Eyes'])) {
if ($ and === 1)
$ Where. = "AND";
 
$ Where. = "Eyes = '$ _GET [Eyes]'";
$ And = 1;
}
 
if (isset ($ _GET ['Hair']) &&! empty ($ _GET ['Hair'])) {
if ($ and === 1)
$ Where. = "AND";
 
$ Where. = "Hair = '$ _GET [Hair]'";
$ And = 1;
}
 

 

if (strlen ($ where)> 6)
$ Query. = $ Where;
 
$ Result = mysql_query ($ query, $ con);
 

 
if ($ result) {
$ Nrows = mysql_num_rows ($ result);
 
if ($ nrows> 0) {
$ O ='';
 
while ($ row = mysql_fetch_assoc ($ result)) {
 
$ O. = "$ Row [image]";
}
 
$ O. = "";
Else {}
$ O = 'There were no results';
}
Else {}
$ O = 'Error: Unable to run the query. '. mysql_error ($ con);
}
 
mysql_free_result ($ result);
mysql_close ($ con);
echo $ o. "";
exit;
?>
Posted 27-Feb-13 3:07am
Edited 27-Feb-13 5:16am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Order By Rate Desc, Age Asc, Height Desc, Weight Asc

See examples in Help file at ORDER BY Clause[^]
  Permalink  
Comments
carloscontreras at 27-Feb-13 11:20am
   
adapt it to my code
carloscontreras at 27-Feb-13 13:36pm
   
I have tried for days to sort query results without result

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 215
1 Sebastiaan Meijerink 141
2 Sergey Alexandrovich Kryukov 124
3 Rana Waqas 85
4 Maciej Los 84
0 Sergey Alexandrovich Kryukov 6,558
1 OriginalGriff 6,288
2 Peter Leow 2,534
3 Abhinav S 2,358
4 Maciej Los 2,352


Advertise | Privacy | Mobile
Web04 | 2.8.150414.1 | Last Updated 27 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100