0

Banging my head against a wall here ... Been stuck on this for a while now ...

I have this query:

    include 'dbconnect.php';
    $user1id=$_SESSION['userid'];
    $stmt=$db->prepare("SET @chatroom_ID=(SELECT ID FROM chatroom
                        JOIN chatroommembers ON chatroom_ID=chatroom.ID
                        WHERE chatroommembers.user_ID=:user1id
                        ORDER BY chatroom.ID
                        LIMIT 1);
                        SELECT user_ID FROM chatroommembers 
                        WHERE chatroom_ID=@chatroom_ID 
                        AND user_ID!=:user1id
                        LIMIT 1;");
    $stmt->bindValue(':user1id',$user1id,PDO::PARAM_INT);
    $stmt->execute();
    $user2id=$stmt->fetchColumn();
    echo $user2id;

What it is supposed to do, is find the chatroom ID that the current user is in, and echo out the user id of the second person that is in the room (There is always only 2 in the room)

The query works in PHPmyadmin ... But not in my PHP script

I can echo $user1id no problems

If I replace the query with a simple SELECT statement it works, for example:

SELECT user_ID FROM chatroommembers WHERE user_ID=:user1id

I've tried pretty much everything I can think of, and this statement just doesnt want to work ...

No errors are being outputted (I've even made it wrong to make sure my errors will output, which they did).

6
  • 7
    that's not one query, its two Commented Feb 26, 2014 at 21:56
  • It's worked that way in other queries I've done, where I need to SET a value first Commented Feb 26, 2014 at 22:00
  • Maybe try separating it into two queries and see if that helps? Commented Feb 26, 2014 at 22:00
  • no it doesn't. phpmyadmin splits it in to two queries Commented Feb 26, 2014 at 22:01
  • Also, PDO doesn't deal with duplicate ":user1id" tokens (edit: this might only be in some versions bugs.php.net/bug.php?id=40417) Commented Feb 26, 2014 at 22:01

1 Answer 1

1

As Dagon has mentioned in the comments, you've got two queries going on there. PHP's MySQL libraries will, by default, limit you to only being able to use one query at a time - mainly to prevent SQL injection from multiple queries.

Howevever, mysqli has a multi_query function which has support for this, and in your case PDO can support multiple queries with PDOStatement::newRowset.

There are other options for you too:

I've had to deal with this occasionally, especially when dealing with temporary tables. Using SQL transactions and/or simply splitting each query into a seperate PHP statement will do the trick.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot dude :D ... Didn't know about nextRowset() ... My head is hurting a little less now :D
Cool - check the second link in the bullet points too, it's got another example for PDO

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.