# Miscellaneous > Ask an Expert >  Newbie needs help with query not working

## mogsta22

Hi there,
can an expert help me out here. I am very new to writing Database queries and in honesty I am not at all sure what I am doing.

I have a script in PHP that should query the database and the resulting output should produce a .CSV file. Whenever I run the script I get the following error message:

processing order ?ordernum=nnn using template template-ebay.html...Connected to database..
You have an error in your SQL syntax near 'FROM categories_description, orders_products , products , products_to_categories' at line 2
Couldn't execute query: SELECT orders_products.orders_id, orders_products.products_model, orders_products.products_name, products.products_image, orders_products.final_price, categories_description.categories_name, products_description.products_description, FROM categories_description, orders_products , products , products_to_categories, products_description WHERE products_description.products_id = products.products_id AND products.products_id = orders_products.products_id AND products_to_categories.products_id = orders_products.products_id AND products_to_categories.products_id = products.products_id AND categories_description.categories_id = products_to_categories.categories_id AND (orders_products.orders_id=?ordernum=nnn) GROUP BY products.products_model ORDER BY orders_products.orders_id

The query is exactly as stated above in the error message. I am going round in circles trying to sort it out. Can anyone here help me out with it please.

regards

Mogsta

----------


## oraclable

You don't need a comma just before the FROM :
And could you tell us why you put this? "AND (orders_products.orders_id=?ordernum=nnn)"


Give it a try with this:

SELECT orders_products.orders_id, orders_products.products_model, orders_products.products_name, products.products_image, orders_products.final_price, categories_description.categories_name,products_de  scription.products_description FROM categories_description, orders_products , products , products_to_categories, products_description WHERE products_description.products_id = products.products_id AND products.products_id = orders_products.products_id AND products_to_categories.products_id = orders_products.products_id AND products_to_categories.products_id = products.products_id AND categories_description.categories_id = products_to_categories.categories_id  GROUP BY products.products_model ORDER BY orders_products.orders_id

----------


## mogsta22

Many thanks for the response. It was indeed the comma which caused the problem.

regards

Mogsta

----------


## zeverdoosje

Hello,

I'm struggling with the same problem.

Can somebody tell me what's wrong?
Here's part of the php-script:

$rasgroep = $_POST['selectrasgroep'];
$sekse = $_POST['sekse'];
$leeftijd = $_POST['leeftijd'];
$anderekatten = $_POST['anderekatten'];
$kanbijhonden = $_POST['kanbijhonden'];
$kinderen = $_POST['kinderen'];
$binnenblijven = $_POST['binnenblijven'];
$buiten = $_POST['buiten'];

$query = " SELECT nr, naam, sekse ";
$query = query . " FROM kat ";
//rasgroep
if ($rasgroep != "alles") {
    $query = query . " WHERE rasgroep = \"$rasgroep\"; ";
}

//sekse
if ($sekse != "maaktnietuit" ) {
    $query = query . " AND sekse = \"$sekse\"; ";
}


//leeftijd
if ($leeftijd != "maaktnietuit" ) {
    $query = query . " AND leeftijd = \"$leeftijd\"; ";
}


//anderekatten
if ($anderekatten != "maaktnietuit" ) {
    $query = query . " AND anderekatten = \"$anderekatten\"; ";
}

//kanbijhonden
if ($kanbijhonden != "maaktnietuit" ) {
    $query = query . " AND kanbijhonden = \"$kanbijhonden\"; ";
}

//kinderen
if ($kinderen != "maaktnietuit" ) {
    $query = query . " AND kinderen = \"$kinderen\"; ";
}

//binnenblijven
if ($binnenblijven != "maaktnietuit" ) {
    $query = query . " AND binnenblijven = \"$binnenblijven\"; ";
}

//buiten
if ($buiten != "maaktnietuit" ) {
    $query = query . " AND buiten = \"$buiten\"; ";
}



This is one of the errors I keep getting: You have an error in your SQL syntax near 'query AND leeftijd = "volwassen"; ' at line 1.query AND leeftijd = "volwassen"; 


Thanks in advance

----------


## oraclable

(BAD)
$query = *query*  . " WHERE rasgroep = \"$rasgroep\"; ";


(GOOD)
$query = *$query*  . " WHERE rasgroep = \"$rasgroep\"; ";

So, do not forget to replace "query" by "$query" 9 times in your script..  :Smilie: 



Note: Before you duplicate a line in your program make sure it original works 


Good luck!

----------

