简体   繁体   中英

SQL error 1064 when adding line breaks

I have a an query that I want to work on so I formatted it in an easier way to read. However when I refresh my page I receive the error 1064. The only thing I have done is add line breaks. Even if I only add 1 line break I get this error.

Here is the original query:

$listing_sql = "select " . $select_column_list . " p.products_id, p.products_model, p.manufacturers_id, p.products_price, p.products_tax_class_id, IF(s.status, s.specials_new_products_price, NULL) as specials_new_products_price, IF(s.status, s.specials_new_products_price, p.products_price) as final_price from " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_PRODUCTS . " p left join " . TABLE_MANUFACTURERS . " m on p.manufacturers_id = m.manufacturers_id left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p.products_status = '1' and p.products_id = p2c.products_id and pd.products_id = p2c.products_id and pd.language_id = '" . (int)$languages_id . "' and p2c.categories_id = '" . (int)$current_category_id . "'";

and simply adding 1 or 2 line breaks before FROM like so:

$listing_sql = "select " . $select_column_list . " p.products_id, p.products_model, p.manufacturers_id, p.products_price, p.products_tax_class_id, IF(s.status, s.specials_new_products_price, NULL) as specials_new_products_price, IF(s.status, s.specials_new_products_price, p.products_price) as final_price 

from " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_PRODUCTS . " p left join " . TABLE_MANUFACTURERS . " m on p.manufacturers_id = m.manufacturers_id left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p.products_status = '1' and p.products_id = p2c.products_id and pd.products_id = p2c.products_id and pd.language_id = '" . (int)$languages_id . "' and p2c.categories_id = '" . (int)$current_category_id . "'";

Causes the error to come up I am using wamp server with mysql version 5.5.16 and php version 5.2.2 appache 2.0.63

it's because you're literally adding in a \\n or a large gap into the query.

If you stopped the quotes at as final_price , then added line breaks, and then continued the string . 'from ' . TABLE... . 'from ' . TABLE... . 'from ' . TABLE... , you should find the error doesn't occur.

type this code directly after the query, and you should see your error.

echo $listing_sql;
exit();

You put the newline in the middle of a string, which the mysql server won't like. A sane way to break your query into multiple lines would be:

$listing_sql = "select " . $select_column_list . 
" p.products_id, p.products_model, p.manufacturers_id, p.products_price, " .
"p.products_tax_class_id, IF(s.status, s.specials_new_products_price, NULL) as " .
"specials_new_products_price, IF(s.status, s.specials_new_products_price, " .
"p.products_price) as final_price from " . 
TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_PRODUCTS . " p left join " . 
TABLE_MANUFACTURERS . " m on p.manufacturers_id = m.manufacturers_id left join " . 
TABLE_SPECIALS . " s on p.products_id = s.products_id, " . 
TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p.products_status = '1' and " .
"p.products_id = p2c.products_id and pd.products_id = p2c.products_id and " .
"pd.language_id = '" . (int)$languages_id . "' and p2c.categories_id = '" . 
(int)$current_category_id . "'";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM