Jump to content

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syn


excelmaster
Go to solution Solved by Jacques1,

Recommended Posts

Hello all,

 

Appreciate if you folks could pls. help me understand (and more importantly resolve) this very weird error:

 

 

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 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 'ASC, purchase_later_flag ASC, shopper1_buy_flag AS' at line 3' in /var/www/index.php:67 Stack trace: #0 /var/www/index.php(67): PDO->query('SELECT shoplist...') #1 {main} thrown in /var/www/index.php on line 67

 

Everything seems to work fine when/if I use the following SQL query (which can also be seen commented out in my code towards the end of this post) :

 

 

$sql = "SELECT shoplist.*, store_master.store_name, item_master.item_name
           FROM shoplist, store_master, item_master
           WHERE     shoplist.store_id = store_master.store_id
                   AND shoplist.item_id = item_master.item_id";

 

However, the moment I change my query to the following, which essentially just includes/adds the ORDER BY clause, I receive the error quoted above:

$sql = "SELECT shoplist.*, store_master.store_name, item_master.item_name 
		FROM shoplist, store_master, item_master 
		ORDER BY purchased_flag ASC, 
				purchase_later_flag ASC, 
				shopper1_buy_flag ASC, 
				shopper2_buy_flag ASC, 
				store_name ASC) 
		WHERE 	shoplist.store_id = store_master.store_id 
				AND shoplist.item_id = item_master.item_id";

In googling for this error I came across posts that suggested using "ORDER BY FIND_IN_SET()" and "ORDER BY FIELD()"...both of which I tried with no success.

 

Here's the portion of my code which seems to have a problem, and line # 67 is the 3rd from bottom (third last) statement in the code below:

 

 

                    <?php
                        /*
                        $sql = "SELECT shoplist.*, store_master.store_name, item_master.item_name
                                FROM shoplist, store_master, item_master
                                WHERE     shoplist.store_id = store_master.store_id
                                        AND shoplist.item_id = item_master.item_id";
                        */
                    
                        $sql = "SELECT shoplist.*, store_master.store_name, item_master.item_name
                                FROM shoplist, store_master, item_master
                                ORDER BY FIND_IN_SET(purchased_flag ASC,
                                                 purchase_later_flag ASC,
                                                 shopper1_buy_flag ASC,
                                                 shopper2_buy_flag ASC,
                                                 store_name ASC)
                                WHERE     shoplist.store_id = store_master.store_id
                                        AND shoplist.item_id = item_master.item_id";
                        $result = $pdo->query($sql);                        
                        
                        // foreach ($pdo->query($sql) as $row) {
                        foreach ($result as $row) {
                            echo '<tr>';
                                print '<td><span class="filler-checkbox"><input type="checkbox" name="IDnumber[]" value="' . $row["idnumber"] . '" /></span></td>';

 

 

Thanks

 

Link to comment
Share on other sites

Honestly.....I hadn't a clue....and obviously I didn't think that it would matter as to where the "WHERE" was placed, in relation to the "ORDER BY" clause...but of course, I couldn't have been more wrong!

 

And of course...my bad for not checking the MySQL manual.

 

Thank you Sir...for pointing this out to me, and for providing me the link to the SELECT syntax, which I've now bookmarked.

 

Good day!

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.