Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysqli, OOP vs Procedural

Tags:

php

pdo

mysqli

I just saw this for the first time. I had no idea you can instantiate a mysqli class by doing something like

new mysqli( $host, $username, $password, $db );

This is brand new to me...Every tutorial I have seen online or in books when connecting to database does something like this:

$conn = mysqli_connect( $host, $username, $password, $db );

if ( !$conn ) {
    die( 'Sorry, could not connect');
}

Is there a reason why most tutorials do it the procedural way? Even in tutorials I have seen on creating a separate database class, tell me to use the procedural style code...

What's the difference with mysqli procedural vs oop? Is there any? Do I have to install any extensions of some kind or can I just start using in my code? Total newb here, so not really sure.

Also, when is it a good idea to use PDO vs mysqli? Is PDO used most commonly by developers that work on large scale Enterprise applications? Or is simply a matter of preference.

Thanks for any help...

learning php has quickly become addictive...I just want to know more and more.

like image 580
kdub Avatar asked Jun 02 '11 01:06

kdub


2 Answers

99% of PHP tutorials on the internet are outdated, advocate terrible code or are written by people that probably shouldn't teach PHP (and commonly all of the above). You should find the handful of good ones.

PDO is generally favoured over mysqli by developers, including me. For one, it can handle many more databases than just MySQL.

like image 198
alex Avatar answered Oct 07 '22 12:10

alex


To answer your questions from the practical point of view

Is there a reason why most tutorials do it the procedural way?

Enough to say that reasons are not technology related.

What's the difference with mysqli procedural vs oop? Is there any?

No.

Do I have to install any extensions

No (save for mysqli itself, of course).

Also, when is it a good idea to use PDO?

Yes. As a matter of fact PDO would be the only sane choice.

You have to understand that set of mysqli functions is just a low level API, not intended to be used as is. One have to implement a wrapper upon mysqli, and then use this wrapper's methods. While PDO is such a wrapper already, offering help with performing many tasks. I wrote a guide on PDO, that emphasizes its benefits and discloses some superstitions, where, I hope, you will find answers for many questions you surely have.

like image 20
Your Common Sense Avatar answered Oct 07 '22 13:10

Your Common Sense