Jump to content

Problem with "Order BY"


SamCec

Recommended Posts

Hi: I'm new to PHP and Mysql. Wondering if you can help? This code works without the "order by" clause but with it, I get a parsing error. I do want the records sorted by "objectname", I also tried "ordered by  $item['objectName']". That didn't work either. I got the same error.

$selectObjects =    "SELECT * FROM `objecten` [color=red]ORDER BY $item['objectName']";[/color]
$queryObjects = mysql_query($selectObjects) or die(mysql_error());

while($item = mysql_fetch_array($queryObjects))
{
    array_push($objects, array(    $item['objectName'], split(";", $item['photos']), $item['price'], $item['base'], 
                                $item['centerPiece'], $item['name'], $item['wood']));
}

Link to comment
Share on other sites

Thanks for the suggestion but I pasted the code you supplied in my application and got this error: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1"

 

Sam

Link to comment
Share on other sites

Hi

 

What is the value of $item['objectName']? It should be a column name from the query.

 

Change you or die to:-

 

$queryObjects = mysql_query($selectObjects) or die(mysql_error()." $selectObjects");

 

This should mean you can see what the SQL generated looks like.

 

All the best

 

Keith

Link to comment
Share on other sites

KickStart:

 

Here is the error I received after changing the "die" clause:

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 SELECT * FROM `objecten` ORDER BY

Link to comment
Share on other sites

Folks: let me re-explain: Someone else wrote this for me because I DON'T KNOW php. Here are the 1st 28 lines of the app. PLEASE SEE MY COMMENTS AFTER THIS CODE.

<?php

include('connect/connect.php');
mysql_select_db($database) or die ("Unable to Connect to the database");

$woods = array();

$selectWood =    "SELECT * FROM `wood`";
$queryWood = mysql_query($selectWood) or die(mysql_error());

while($item = mysql_fetch_array($queryWood))
{
    $woods[$item['index']] = array($item['name'], $item['photo']);
}

$objects = array();

$selectObjects =    "SELECT * FROM `objecten` ORDER BY ".$item['objectName']." ";

$queryObjects = mysql_query($selectObjects) or die(mysql_error()." $selectObjects");

while($item = mysql_fetch_array($queryObjects))
{
    array_push($objects, array(    $item['objectName'], split(";", $item['photos']), $item['price'], $item['base'], 
                                $item['centerPiece'], $item['name'], $item['wood']));
}

without the ORDER BY clause the code works fine. I know a little SQL so I originally added the "order by". When it didn't work, that's when I came for help.

 

Sam

Link to comment
Share on other sites

The logic looks fatally flawed to me. The ORDER BY in the second query can only ever have the value of $item['objectName'] from the last record returned from the previous query. Unless your `objecten` table has a field name that corresponds to every possible value in the `objectName` field of the `woods` table, this ain't gonna work (and if it does have all of those values as field names, the database design is probably pretty scary).

 

Maybe if you post the structure of the two tables, some representative data, and a concise description of what you're trying to accomplish, someone will be able to better help you with it.

Link to comment
Share on other sites

It's going to take me some time to figure how to do what your asking. I am aware that "objectname" is populated. There is a record for each object. The objects are: Bunny, Butterfly, Cardinal, etc.

You can see all of the objects by going to: www.woodartbysam.com/test If you go there, please keep in mind there is still a lot of work to be done on the site.

 

Sam

Link to comment
Share on other sites

Hi

 

Starting from the beginning.

 

In php a variable has a name that starts with a $. You can have arrays of variables, which is what $item['objectName'] is.

 

What your current code is trying to do is to order the returned records from the SQL in the order of the VARIABLE $item['objectName']. If $item['objectName'] has a value of fred then the SQL will try and order the returned rows in the ascending order of the column called fred. If you do not have a column called fred then you will get an error.

 

I suspect that objectName is a column on your second query, and I suspect that you always want the data returned in that order. As such you probably want something like this:-

 

$selectObjects =    "SELECT * FROM `objecten` ORDER BY objectName ";

 

All the best

 

Keith

Link to comment
Share on other sites

Edit: Basically says the same as above ^^^^

 

I'm going to take a wild guess here. Do you want to add an ORDER BY to the `objecten` table because you want the records ordered by the objectName column that is in that table?

 

If so, your query would be -

 

$selectObjects =    "SELECT * FROM `objecten` ORDER BY objectName";

 

We only see the information that you supply in your posts. When you post code that contains a php variable $item['objectName'] somewhere in it, we have to assume that IS what you wanted to put into the code for some reason that you have, especially if you don't state what it is you are trying to accomplish.

 

It is always best to state what it is you are trying to accomplish, not just that something you are showing us does not work.

Link to comment
Share on other sites

KickStart and PFMaBiSmAd:

Sorry for the delay in getting back to you, I had to go out.

 

Thank you so much for the corrected code and the explanation. Your code did the trick. I now have the items in alphabetic order.

 

Thanks Again,

Sam

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.