Link to home
Start Free TrialLog in
Avatar of Sheldon Livingston
Sheldon LivingstonFlag for United States of America

asked on

Order by clause

I've inherited the code below:
//Get manufacturers
$query = "select * from manufactures order by manufacturer";
$manufacturers = sqlsrv_query($objConnection, $query);
if($manufacturers === false) {
    die( print_r( sqlsrv_errors(), true) );
}

Open in new window

What if you really wanted the "order by manufacturer" portion to work.  Is it possible?
Avatar of Brian Tao
Brian Tao
Flag of Taiwan, Province of China image

What do you mean?  Doesn't it work?  It looks fine.
What are the diagnostics or error messages you get?
Looks fine to me too.  "order by manufacturer" implies that there is a column called 'manufacturer' in the results.  "order by" is used to 'order' the results.
Avatar of Sheldon Livingston

ASKER

It doesn't error out... but also doesn't change the sort order.  Adding ASC or DESC does nothing.

Changing manufacturer to manufacturerxxx kills the query (as expected).

Is there some kind of a $Params syntax?
I'm sorry but 'order by' has always worked that way and I have it working that way on a dozen servers here, both MS SQL and MySQL.  Something else is going on.  Are you getting any error messages?
Since the SQL statement itself looks good, I guess you're having sorting issues when displaying the result.  Can you show us other part of the PHP script?
This is PHP working on a MS SQL server.

This query:
//Get manufacturers
$query = "select * from manufactures order by manufacturer";
$manufacturers = sqlsrv_query($objConnection, $query);
if($manufacturers === false) {
    die( print_r( sqlsrv_errors(), true) );
}

Open in new window


is used in:
<div class="col-md-3">
	<select id="manufacturer" name="manufacturer" class="selectpicker" style="width: 174px">
		<option>Please Select</option>
		<?php
			while( $row = sqlsrv_fetch_array( $manufacturers, SQLSRV_FETCH_ASSOC) ) {
				echo "<option>" . $row['manufacturer']. "</option>";
			}

			sqlsrv_free_stmt( $manufacturers);
		?>
	</select>
</div>

Open in new window


No errors are thrown... simply just doesn't adjust the order.
Time to show us your data from the "View Source" in your browser.  The only reason I can think of that would make it not work is leading spaces in the data or something odd like that.
I notice that your 'option' tags do not have a 'value' in them.  http://www.w3schools.com/tags/tag_select.asp
Agree with Dave.  Either create the SSCCE so we can test it ourselves, or at least show us the view source  so we can see what is present in the generated HTML. thanks.
I just ran a test on one of my simple 'sqlsrv' PHP files with this query:
"SELECT TOP 10 ent_num, DisplayName, Sortname, WebSite, Descript, Cat, Approved FROM  dbo.websitelist WHERE ent_num < 11 ORDER BY ent_num"

Open in new window

I can put any of the fields as the ORDER BY and it sorts properly every time.
Show us a sample of the values in your manufacturer column. Does this column contain names or id numbers?
Kim... just names
Here is the view source:
<div class="row">
			<div class="col-md-2 col-md-offset-4">
				<span>Manufacturer </span>
			</div>
			<div class="col-md-3">
				<select id="manufacturer" name="manufacturer" class="selectpicker" style="width: 174px">
					<option>Please Select</option>
					<option>Toshiba</option><option>Micro</option><option>Hitachi</option><option>Texas Instruments</option><option>Xilinx</option><option>Cypress</option><option>VLSI</option><option>Phillips</option><option>NXP</option><option>Hyundai</option><option>Samsung</option><option>MOT</option><option>DALE/VISHAY</option><option>YAGEO</option><option>BC COMPONENTS</option><option>KEMET</option><option>KOA</option><option>AVX</option><option>GRYPHON</option><option>FREESCALE SEMICONDUTOR</option><option>AMPHENOL</option><option>JDS UNIPHASE</option><option>MEAN WELL</option><option>FCI</option><option>MOLEX</option><option>AMP</option><option>LINEAR TECHNOLOGY</option><option>PERICOM</option><option>SCHAFFNER</option><option>DALLAS</option><option>MAXIAM</option><option>GTRAN</option><option>EXCELSYS</option><option>FINISAR</option><option>ATI</option><option>ATI</option><option>DATATRONIC</option><option>ARTESYN</option><option>GTRAN</option><option>ASTEC</option><option>WUHAN TELECOMUNCATIONS DEVICES</option><option>SIEMENS</option><option>CONEXANT</option><option>TYCO</option><option>ERICSSON</option><option>AIRPAX</option><option>OPNEXT</option><option>TELLLABS</option><option>EXCELSYS</option><option>LSILOGIC</option><option>BROADCOM</option><option>EPCOS</option><option>PILKOR ELECTRONICS</option><option>COEV</option><option>PANJIT</option><option>TYCO</option><option>RAYCHEM</option><option>MURATA</option><option>SPRAGUE</option><option>PULSE</option><option>LITTLE FUSE</option><option>TDK</option><option>CYPRESS</option><option>NXP</option><option>PHILLIPS</option><option>MICRONAS</option><option>CHIPS</option><option>ALTERA</option><option>ACTEL</option><option>ACTEL</option><option>LSI</option><option>PANASONIC</option><option>TRIAD</option><option>FOXCONN</option><option>GRAYHILL</option><option>ACCELINK TECH</option><option>SOURCE PHOTONICS</option>				</select>
				<!--input id="manufacturer" name="manufacturer"-->
			</div>
		</div>

Open in new window

Here is sample data (ID and manufacturer):
77      ACCELINK TECH
70      ACTEL
71      ACTEL
46      AIRPAX
69      ALTERA
26      AMP
21      AMPHENOL
38      ARTESYN
40      ASTEC
35      ATI
36      ATI
18      AVX
15      BC COMPONENTS
52      BROADCOM
68      CHIPS
55      COEV
43      CONEXANT
64      CYPRESS


and design:

User generated image
What appears in the HTTP request when you submit the selection?  You can use PHP var_dump($_REQUEST) to find out.  Please get that and post it here, thanks.
Ray... this issue is on a page that contains a form.  The code is in the form trying to order a list of manufacturers.  

I put the dump code at the top of that page to get:

array(0) { }
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I agree.... I changed $manufacturers to $NEWmanufacturers and it doesn't load anything now.  Working on it.
Reading the code from your "View Source".  It's ordering by the ID column.
That's weird, is it possible that you attach your PHP script here and not just paste part of it?  Maybe all you need is just another pair of eyes.
:-/  Found an "include" statement...  Thanks for the assist!
Yep - that'll catch you out :)

Happy you're sorted.
Great catch Chris