Page 1 of 1

What is the difference between these two?

Posted: Fri Feb 25, 2011 1:04 pm
by someguyhere

Code: Select all

	$mysqli->query("SELECT ID FROM wp_posts");

Code: Select all

	$query = "SELECT ID FROM wp_posts";

Re: What is the difference between these two?

Posted: Fri Feb 25, 2011 1:05 pm
by danwguy
the first one will run the query the second one just stores the statement in a variable that you have to use later to run the query

Re: What is the difference between these two?

Posted: Fri Feb 25, 2011 1:18 pm
by someguyhere
Ok. I know that the first one is also OOP, but is there any benefit to using one over the other? If so, what circumstances might one be a better fit?

Re: What is the difference between these two?

Posted: Fri Feb 25, 2011 1:31 pm
by danwguy
Depends on what you are trying to do, if you are using OOP then you should go with the first one. I usually use.

Code: Select all

$result = mysql_query("SELECT ID FROM wp_posts) or die("Could not SELECT: " .mysql_error());
but like I said if you are using OOP already then by all means, use the first one. if you are starting out with php and not very good with OOP, it's a tough road. I am actually working on learning it now. It is very useful in some scenarios, and not so in others. I know a lot of people will disagree with that statement, but that's how I feel.

Re: What is the difference between these two?

Posted: Fri Feb 25, 2011 9:17 pm
by Eran
There are more differences between the mysqli and mysql extensions than just syntax. mysqli functions can be used in procedural style as well. mysqli stands for mysql improved extension. You can read more about it in the manual - http://www.php.net/manual/en/mysqli.overview.php

Also, I'd recommend against using die() statements like that in any real project - it leaves no opportunity for error handling.