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?

Firas Al Mannaa
  • 916
  • 1
  • 11
  • 30

5 Answers5

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.

jeroen
  • 91,079
  • 21
  • 114
  • 132
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
M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
  • thansk, but this is not my question! im asking about the result :) – Firas Al Mannaa Apr 01 '14 at 15:53
  • 1
    @FirasAlMannaa Not sure but i guess you are looking for multiple queries to run at once http://stackoverflow.com/questions/6346674/pdo-support-for-multiple-queries-pdo-mysql-pdo-mysqlnd – M Khalid Junaid Apr 01 '14 at 15:55
  • 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. – Kryten Apr 01 '14 at 15:58
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.

spencer7593
  • 106,611
  • 15
  • 112
  • 140
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.

Bjoern
  • 15,934
  • 4
  • 43
  • 48
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.

Dissident Rage
  • 2,610
  • 1
  • 27
  • 33
  • I almost wondered from looking at the original question... why not just an OR? That is if it's all from the same table. – Mattt Apr 01 '14 at 15:59
  • 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. – Dissident Rage Apr 01 '14 at 16:00