1

I have this query

$query = "Select * FROM table WHERE table.firs_column = 1;
Select * FROM table WHERE table.second_column = 1;
Select * FROM table WHERE table.third_column = 1;
Select * FROM table WHERE table.column = 1";

$stmt   = $db->prepare($query);
$result = $stmt->execute();

I want to have multiple results, each one have the result of one query! how to do it?

1
  • 1
    Maybe this example is overly simplified, but you can just use OR here. Commented Apr 1, 2014 at 15:53

5 Answers 5

2

It looks like you are using PDO, so you could do something like:

$first_set = $stmt->fetchAll(PDO::FETCH_ASSOC);

$stmt->nextRowset();
$second_set = $stmt->fetchAll(PDO::FETCH_ASSOC);

$stmt->nextRowset();
$third_set = $stmt->fetchAll(PDO::FETCH_ASSOC);

$stmt->nextRowset();
$fourth_set = $stmt->fetchAll(PDO::FETCH_ASSOC);

To get your 4 rowsets.

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

2 Comments

It's always nice when people don't over think the problem.
It answers my question ... I was testing it, thanks :)
1

You can use UNION if table is same for your multiple queries

Select * FROM table WHERE table.firs_column = 1
UNION
Select * FROM table WHERE table.second_column = 1
UNION
Select * FROM table WHERE table.third_column = 1
UNION
Select * FROM table WHERE table.column = 1

3 Comments

thansk, but this is not my question! im asking about the result :)
@FirasAlMannaa Not sure but i guess you are looking for multiple queries to run at once stackoverflow.com/questions/6346674/…
You're either going to have to run the queries one at a time and get the results one at a time OR combine them all into one query (as @MKhalidJunaid has illustrated) and get one result set with all the results.
1

After you finish with the first result set, use nextRowset() method to advance to the next result set.

You need to have a PHP MySQL driver extension that supports this method.

Comments

0

Not sure what you're aiming at, but did you try UNION?

Your SQL statement would look like this:

SELECT * FROM table WHERE table.firs_column = 1
UNION
SELECT * FROM table WHERE table.second_column = 1
UNION
SELECT * FROM table WHERE table.third_column = 1
UNION
SELECT * FROM table WHERE table.column = 1;

Please show your desired result if you think of something different.

Comments

0

Since we don't know anything about your database structure, I suggest looking into mysqli::multi_query().

If you're trying to pull related data, I highly suggest you look into doing MySQL JOINs instead. MySQL is another language unto itself that should be learned as a distinct language rather than just a string to be contatenated in PHP.

2 Comments

I almost wondered from looking at the original question... why not just an OR? That is if it's all from the same table.
If he's pulling the same data from the same table but depending on different conditions, yeah, but more often than not it seems people use multiple queries for fetching different, related result sets.

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.