sql_query syntax error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

sql_query syntax error

Post by Jim_Bo »

Hi,

The follwoing peice of code:

Code: Select all

 
    $query = mysql_query('SELECT * FROM stock
    INNER JOIN manufacturers on manufacturers.mid=stock.mid
    INNER JOIN colors on colors.cid=stock.cid
    INNER JOIN sizes on sizes.sid=stock.sid
    INNER JOIN products on products.pid=stock.pid
    WHERE stock.stid IN (';
    foreach($_SESSION['cart'] as $key => $value) {
    $query .= $key . ',';
    }
    $query = substr ($query, 0, -1) . ') ORDER BY stock.stid ASC';
    $result = mysql_query($query);
 
Gives the an error on this line:

Code: Select all

 
WHERE stock.stid IN (';
 
 

Code: Select all

 
Parse error: syntax error, unexpected ';' in /home
 
Cant figure it out..

Thanks
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: sql_query syntax error

Post by requinix »

Let's remove the query and see what's left:

Code: Select all

   $query = mysql_query('query';
    foreach($_SESSION['cart'] as $key => $value) {
    $query .= $key . ',';
    }
    $query = substr ($query, 0, -1) . ') ORDER BY stock.stid ASC';
    $result = mysql_query($query);
You see the problem now?
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Re: sql_query syntax error

Post by Jim_Bo »

I dont sorry..

I kno that this works..

Code: Select all

 
    $query = 'SELECT * FROM stock WHERE stid IN (';
    foreach($_SESSION['cart'] as $key => $value) {
    $query .= $key . ',';
    }
    $query = substr ($query, 0, -1) . ') ORDER BY stid ASC';
    $result = mysql_query($query);
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: sql_query syntax error

Post by requinix »

Let's zoom in, shall we?

Code: Select all

$query = mysql_query('query';
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Re: sql_query syntax error

Post by Jim_Bo »

Im still not seing it..

What i dont get is the fact that this work fine..

Code: Select all

 
$query = 'SELECT * FROM stock WHERE stid IN (';
foreach($_SESSION['cart'] as $key => $value) {
$query .= $key . ',';
}
$query = substr ($query, 0, -1) . ') ORDER BY stid ASC';
$result = mysql_query($query);
 
The only difference between that and the first post is the query..

Maybe some extra clues then ?

Im lost ahve spent hours on such a small issue

Thanks
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: sql_query syntax error

Post by Chris Corbyn »

You can't split function calls across multiple statements. It's already been pointed out to you that this won't work. You're terminating your function call before you've finished making it (semicolon forms end of statement).

Code: Select all

$query = mysql_query('query';
Also, change your foreach() followed by substr() for a simple implode(',', $_SESSION['cart']).
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Re: sql_query syntax error

Post by Jim_Bo »

Im lost..

Can I get a bit more example code to get on the right track please..

Thanks
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: sql_query syntax error

Post by novice4eva »

hehe he simply doesn't see it, well happens :)

Code: Select all

 
  $query = mysql_query('SELECT * FROM stock
     INNER JOIN manufacturers on manufacturers.mid=stock.mid
     INNER JOIN colors on colors.cid=stock.cid
     INNER JOIN sizes on sizes.sid=stock.sid
     INNER JOIN products on products.pid=stock.pid
     WHERE stock.stid IN (';
 
tasairis and chris were trying to say that you have called mysql_query('your query without well a closing bracket but also an incomplete query'; It should have been

Code: Select all

 
  $query = 'SELECT * FROM stock
     INNER JOIN manufacturers on manufacturers.mid=stock.mid
     INNER JOIN colors on colors.cid=stock.cid
     INNER JOIN sizes on sizes.sid=stock.sid
     INNER JOIN products on products.pid=stock.pid
     WHERE stock.stid IN (';
 
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Re: sql_query syntax error

Post by Jim_Bo »

Doh... So simple :oops:

Thanks
Post Reply