tags:

views:

121

answers:

4

This sounds like a really simple question, but I am new to PHP. If I have a statement like this:

$r =& $db->query("insert into table (col1, col2) values (10, 20)");

Do I have to still execute it, or does it get executed when I reference it? I have another case where I have a select query, which seems logically to run only when I call fetchrow, but the code I am copying from does not call execute or fetch. I would have expected it to, so I cannot tell if it is just that I don't get it, or that the missing execute statement is the problem. It also does not insert the record, but it does not throw an error I can find.

Also, I am a little confused by the =& notation. I looked it up on google, and found a few mentions of it, but I am still not clear on it.

Thanks.

+1  A: 

The query gets executed when you call the query function. when you talk about code that needs to be fixed, what is broken, and what does the code that "need[s] to be fixed" (according to who?) look like?

FrustratedWithFormsDesigner
I should not even have added any text about "fixing." That was unrelated, and I will edit it out.
MJB
+2  A: 

It will be executed when you call query()

The =& notation is obsolete... it used to make the function return a reference to the resource object. But current versions of PHP (>5.0, I think) always pass (and return) objects by reference, so it doesn't really change anything anymore.

keithjgrant
ryeguy
+1  A: 

& is used in several contexts and it means by reference. You should start reading from here:

In your code snippet it's most likely unnecessary (although you give little clue about what $db is) because the result set is probably an object and objects no longer need to be assigned by reference since that's the default behaviour. If you are learning PHP, be careful with outdated tutorials.

Álvaro G. Vicario
Thanks. I appreciate the pointers to the manuals. I have been googling pretty much everything, and have not found any sites where I can reliably find what I want. I keep coming back to es.php.net anyway.
MJB
"pointers to the manuals" Pun alert!
keithjgrant
Yeah, well I come from C and Perl, so I thought it was apropos. Thanks for noticing.
MJB
A: 

$db->query is a method contained by a class, it's not functional in php out of context. It is possible that this example of yours comes from a larger application that uses a database abstraction layer like ADODB or any of its kind.

If this is the case, then you could refer to the documentation specific to that db abstraction layer, because the query could be contained in a transaction for example and it would not be executed as soon as you call query();

To be sure the query is executed immediately try testing with a simple mysql function:

mysql_query("SHOW TABLES");
Dragos Badea