What is the difference between these two?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

What is the difference between these two?

Post by someguyhere »

Code: Select all

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

Code: Select all

	$query = "SELECT ID FROM wp_posts";
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: What is the difference between these two?

Post 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
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: What is the difference between these two?

Post 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?
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: What is the difference between these two?

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: What is the difference between these two?

Post 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.
Post Reply