Reputation: 7686
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.
Upvotes: 1
Views: 173
Reputation: 872
$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");
Upvotes: 0
Reputation: 146430
&
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.
Upvotes: 1
Reputation: 12739
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.
Upvotes: 1
Reputation: 27486
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?
Upvotes: 1