tags:

views:

149

answers:

5

When I do:

$q = 'insert into movies values("lion king"); select * from movies;';
$result = $db->query($q);
echo mysqli_num_rows($result);

it says that $result is boolean, not mysqli result. if I check $result like this:

if(!$result) echo 'fail';

it outputs 'fail'.

Is mysql not capable of handling more than 1 query at a time? How do I solve this?

+1  A: 
$db->query('insert into movies values("lion king");');
$db->query('select * from movies;');
Derek Illchuk
So is mysql not able to do those together??
sombe
You really don't want to do those together. You should check that your insert worked and then do the other query.
Arthur Thomas
A: 
$q = 'insert into movies values("lion king")';
$result = $db->query($q);
$q = 'select * from movies';
$result = $db->query($q);
echo mysqli_num_rows($result);
David Oneill
+1 I would recommend *not* using multi-query as some have suggested, because multi-query opens up new types of consequences if one has SQL injection vulnerabilities.
Bill Karwin
@Bill, having a potential SQL injection leak is just a very lame excuse not to use functions.
Jan Jongboom
+1 for Jan. If you've got SQL injection vulnerabilities... fix them. They're a huge problem whether you use mysqli::multi_query or not. You might as well say, of a car with 3 flat tires, "I drive it on the highway, but never above 40 miles an hour. Because driving 65 with flat tires is dangerous."
Frank Farmer
Do people not actually hit the up vote when they comment saying '+1'? I thought that saying +1 was explaining why you up-voted...
David Oneill
A: 

You can use multi_query which is difficult to use, but in general no, you should only use one query at a time.

MindStalker
That's bogus. There are plenty of valid reasons to return multiple recordsets.
Jan Jongboom
+5  A: 

You need to use mysqli::multi_query.

Jacob Relkin
A: 

Don't confuse how you write 'queries' in the MySQL command line interface with how you do it with the API.

The MySQL command just wraps the API with something more shell like.

Using the API, you can really only do one query at a time. Of course the client side of the API could do something smart and interpret the semi-colons, splitting into multiple queries for you, but that probably just isn't that useful for enough people.

rhettg