0

I am trying to get the output from one SQL statement into another SQL statement, although it does not seem to be working. Is there an alternative, and how would I use it?

$id = ($_SESSION["user_id"]);
                $query1 = "SELECT event_id FROM booking where user_id = '$id'"; 
                $result = mysql_query($query1);

                $query2 = "SELECT * FROM event where event_id = '$result'";
                $results = mysql_query($query2);
0

3 Answers 3

2

You can join the tables, try using below syntax

SELECT event.event_id,booking.* 
FROM event
JOIN booking on event.event_id=booking.event_id
WHERE booking.user_id='$id';
Sign up to request clarification or add additional context in comments.

1 Comment

+1 - barring some unusual circumstances, joins are to be preferred over multiple queries.
0

What's about using the one query instead of two:

        $id = ($_SESSION["user_id"]);
        $query2 = "SELECT * FROM event 
                   where event_id IN (SELECT event_id 
                                        FROM booking 
                                       where user_id = '$id')";
        $results = mysql_query($query2);

Comments

0

You can also do this in a single query, using a sub-query, like this;

$id = $_SESSION["user_id"];
$query = "SELECT * FROM event where event_id = (SELECT event_id FROM booking where user_id = '$id' LIMIT 1)"; 
$result = mysql_query($query);
$row = mysql_fetch_array($result);

This is only one db call rather than two, so more efficient.

Comments

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.