1

I have a form where people can submit any and all kinds of MySQL queries and as I am now moving from MySQLi to PDO I am wondering if there is any way to deal with multiple query results using PDO. So let's say that a user submits the following to the PDO query method via an HTML form:

SELECT * FROM `organisation`.`employee`;
SELECT * FROM `organisation`.`salary`;

Is there a way to deal with both results?

tshepang
  • 12,111
  • 21
  • 91
  • 136
HelloWorld
  • 2,375
  • 5
  • 22
  • 21
  • May I ask, why you are moving from mysqli to pdo? – Your Common Sense Mar 29 '14 at 03:53
  • From what I've heard, The PHP Group considers it to be the way forward for accessing databases. There doesn't seem to be any real performance difference although MySQLi may be a bit faster in very rare cirumstances. You get named parameters and PDO just feels more natural and logical. – HelloWorld Mar 29 '14 at 04:13
  • First rumor is a total rubbish. Moreover, mysqli is already ahead by a mile. In fact, even if mysqli will cease further development, PDO will need ten years to reach the level mysqli offer in handling mysql queries. – Your Common Sense Mar 29 '14 at 04:16
  • Second one sounds strange for the task. Why do you need placeholders at all, if you are running just arbitrary sets of queries? – Your Common Sense Mar 29 '14 at 04:17
  • I was talking in general why I'm switching. In this case I don't but still, I don't want to be using two interfaces when one is good enough. – HelloWorld Mar 29 '14 at 04:45
  • Then just look for the appropriate function in the PDO manual that is getting multiple results – Your Common Sense Mar 29 '14 at 04:47
  • That's what I started by doing. When you run a query function it returns a PDOStatement object. I don't see how one would do it with such an object. – HelloWorld Mar 29 '14 at 05:07
  • Well, it seems you have found it. For some reason someone who had no idea of this function existence before, managed to spot it for you. "Stack Overflow - the best online manual reader one can get!" ;) – Your Common Sense Mar 29 '14 at 05:26

1 Answers1

0

Split the entry string by the delimiter of ";", cycle through the released array.

That'd be one way at least.

Here's an alternative.

Here's a link to the PDOStatement::nextRowset page. Read the example.

Alejandro
  • 809
  • 1
  • 10
  • 21
Alex L
  • 470
  • 6
  • 15
  • 1
    But some SQL code like stored procedures can contain multiple delimiters right? That would split the code incorrectly. Also, if there is a semicolon in an insert statement, part of the text, it would requre more advanced parsing to detect. – HelloWorld Mar 29 '14 at 03:25
  • I did not realize you're also looking to perform queries such as those. I've added a link to my answer. – Alex L Mar 29 '14 at 03:29
  • That's what I meant by "any and all kinds of MySQL queries". – HelloWorld Mar 29 '14 at 03:30
  • The page you linked to shows how to execute multiple queries but not how to get with multiple results. – HelloWorld Mar 29 '14 at 05:09
  • Alright, I've edited my answer with something new! Maybe it will help. – Alex L Mar 29 '14 at 05:16
  • I haven't tried the code yet but it seems to be what I'm looking for. I looked at all the methods of the object but because of the unusual terminology (nextRowset) I didn't bother to read about it. I thought it had something to do with rows and I was looknig for something like "nextResultset". – HelloWorld Mar 29 '14 at 05:23
  • Perfect! I'm glad I could help, it did take some looking though since I've never used that method either! – Alex L Mar 29 '14 at 05:35