EvilZone

Programming and Scripting => Web Oriented Coding => : PsychoRebellious January 16, 2014, 01:17:19 PM

: MySQLi OOP approach vs Procedural, Which one do you prefer?
: PsychoRebellious January 16, 2014, 01:17:19 PM
The old school Procedural Approach:
$host_name='xxx'
$username='psychorebellious";
$password="xxxx";
$database="somedb";
$con=mysqli_connect($host_name,$username,$password,$database);

Now the OOP approach
//mysqli is an object type- predefined

$mysqli=new mysqli($hostname,$username,$password,$database);


The OOP approach also replaces some functions with variables(I am unsure if this is the correct word, but they do belong to the object)
Say the Oldschool mysqli_num_rows function that takes the return value of the query as it's parameter, the OOP approach replaces it this way
PROCEDURAL APPROACH
$result=mysqli_query($con,"SELECT*FROM table_name");
$totalrows=mysqli_num_rows($result);

OOP APPROACH
$result=$mysqli->query("SELECT*FROM table_name");
$totalrows=$result->now_rows;

So this is it, I've been using the oldschool way and I recently tried the mysqli OOP approach. As for PHP only I've been using OOP approach (makes things simpler,cleaner and better) but for this mysqli OOP approach, I wonder if that even matters, Why would you not use the old school way instead of the OOP way? Not like that lacked anything.
I am awaiting your precious replies and guidance :)
-Khan.
: Re: MySQLi OOP approach vs Procedural, Which one do you prefer?
: Stackprotector January 16, 2014, 01:55:13 PM
The old school Procedural Approach:
$host_name='xxx'
$username='psychorebellious";
$password="xxxx";
$database="somedb";
$con=mysqli_connect($host_name,$username,$password,$database);

Now the OOP approach
//mysqli is an object type- predefined

$mysqli=new mysqli($hostname,$username,$password,$database);


The OOP approach also replaces some functions with variables(I am unsure if this is the correct word, but they do belong to the object)
Say the Oldschool mysqli_num_rows function that takes the return value of the query as it's parameter, the OOP approach replaces it this way
PROCEDURAL APPROACH
$result=mysqli_query($con,"SELECT*FROM table_name");
$totalrows=mysqli_num_rows($result);

OOP APPROACH
$result=$mysqli->query("SELECT*FROM table_name");
$totalrows=$result->now_rows;

So this is it, I've been using the oldschool way and I recently tried the mysqli OOP approach. As for PHP only I've been using OOP approach (makes things simpler,cleaner and better) but for this mysqli OOP approach, I wonder if that even matters, Why would you not use the old school way instead of the OOP way? Not like that lacked anything.
I am awaiting your precious replies and guidance :)
-Khan.
You haven't really been using OOP if you don't know why you should use the OOP mysqli style. And what is a OOP approach? Did you create a class with some functions or did you really implement a full working MVC model?
: Re: MySQLi OOP approach vs Procedural, Which one do you prefer?
: vezzy January 16, 2014, 03:12:09 PM
Both are bad practice these days. If you're still handling SQL with PHP these days, your safest and most up-to-date bet is to use PDO.
: Re: MySQLi OOP approach vs Procedural, Which one do you prefer?
: Satan911 January 16, 2014, 09:08:58 PM
Both are bad practice these days. If you're still handling SQL with PHP these days, your safest and most up-to-date bet is to use PDO.

Saying handling SQL in PHP is always bad isn't true. It all depends on your high-level architecture. If you have a multi-tier architecture and you have a layer that's hidden from the GUI (web page) and your business layer you can code your data access layer in any language you want and possibly on another server. Now if you choose to use PHP for that layer that's up to you. But you could also do it in Python, Ruby, etc. while everything else on your website is in PHP.
: Re: MySQLi OOP approach vs Procedural, Which one do you prefer?
: Uriah January 17, 2014, 01:25:41 AM
Both are bad practice these days. If you're still handling SQL with PHP these days, your safest and most up-to-date bet is to use PDO.
Wow what is it with you bashing PHP at every opportunity? I do agree with your advice about PDO, though. OP: http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/ (http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/)
: Re: MySQLi OOP approach vs Procedural, Which one do you prefer?
: Stackprotector January 17, 2014, 09:24:16 AM
Wow what is it with you bashing PHP at every opportunity? I do agree with your advice about PDO, though. OP: http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/ (http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/)
Well he bashed PHP alot but i fully agree with him, though there are some cases for example huge datasets which can be best handled at mysqli level instead of pdo.
: Re: MySQLi OOP approach vs Procedural, Which one do you prefer?
: PsychoRebellious January 17, 2014, 05:21:14 PM
I appreciate all of the remarks but my question still remains there. Why would one prefer the MySQLi OOP approach over the procedural one?
: Re: MySQLi OOP approach vs Procedural, Which one do you prefer?
: ande January 17, 2014, 06:03:55 PM
I appreciate all of the remarks but my question still remains there. Why would one prefer the MySQLi OOP approach over the procedural one?

As far as I know there wont be any difference in performance. Guess it boils down to what sort of syntax you prefer. However with the OOP approach you can extend the class with your own functions and structures, which may or may not be handy.