Author Topic: MySQLi OOP approach vs Procedural, Which one do you prefer?  (Read 2096 times)

0 Members and 1 Guest are viewing this topic.

Offline PsychoRebellious

  • Peasant
  • *
  • Posts: 130
  • Cookies: -6
    • View Profile
    • My Rantings
MySQLi OOP approach vs Procedural, Which one do you prefer?
« on: 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.

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: MySQLi OOP approach vs Procedural, Which one do you prefer?
« Reply #1 on: 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?
~Factionwars

Offline vezzy

  • Royal Highness
  • ****
  • Posts: 771
  • Cookies: 172
    • View Profile
Re: MySQLi OOP approach vs Procedural, Which one do you prefer?
« Reply #2 on: 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.
Quote from: Dippy hippy
Just brushing though. I will be semi active mainly came to find a HQ botnet, like THOR or just any p2p botnet

Offline Satan911

  • VIP
  • Knight
  • *
  • Posts: 289
  • Cookies: 25
  • Retired god/admin
    • View Profile
Re: MySQLi OOP approach vs Procedural, Which one do you prefer?
« Reply #3 on: 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.
Satan911
Evilzone Network Administrator

Offline Uriah

  • Sir
  • ***
  • Posts: 454
  • Cookies: 42
  • άξονας
    • View Profile
Re: MySQLi OOP approach vs Procedural, Which one do you prefer?
« Reply #4 on: 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/
« Last Edit: January 17, 2014, 01:25:53 am by Uriah »

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: MySQLi OOP approach vs Procedural, Which one do you prefer?
« Reply #5 on: 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/
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.
~Factionwars

Offline PsychoRebellious

  • Peasant
  • *
  • Posts: 130
  • Cookies: -6
    • View Profile
    • My Rantings
Re: MySQLi OOP approach vs Procedural, Which one do you prefer?
« Reply #6 on: 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?

Offline ande

  • Owner
  • Titan
  • *
  • Posts: 2664
  • Cookies: 256
    • View Profile
Re: MySQLi OOP approach vs Procedural, Which one do you prefer?
« Reply #7 on: 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.
« Last Edit: January 17, 2014, 06:06:09 pm by ande »
if($statement) { unless(!$statement) { // Very sure } }
https://evilzone.org/?hack=true