Solved

is this procedural or object oriented

Posted on 2013-05-21
7
294 Views
Last Modified: 2013-05-22
I know that the php will stop support for mysql connection
but I want to know if this code I am using is procedural mysql connection or object oriented mysql connection

            $fetch = mysql_query("
SELECT * from email_doc where unix_timestamp>'1354308240' order by unix_timestamp
        ");    
  while ( $row = mysql_fetch_object( $fetch ) ) {
  echo $row->real_id;
}

Open in new window

0
Comment
Question by:rgb192
  • 3
  • 2
  • 2
7 Comments
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 39186393
If you post the complete code, we may analyze and tell you.

What you've written is just piece of code from the db execution ... it is just written like mysql functions used etc.,

We cannot say with this code.
0
 

Author Comment

by:rgb192
ID: 39186414
this is the connection added
/* Connect to the database and select database */
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname);

            $fetch = mysql_query("
SELECT * from email_doc where unix_timestamp>'1354308240' order by unix_timestamp
        ");    
  while ( $row = mysql_fetch_object( $fetch ) ) {
  echo $row->real_id;
}

Open in new window

0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 39186418
Its  just  procedural way of writing. If it has to be OOP's  then to be written in a common class and call the methods etc.,
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 39186724
To clarify, see here for example:
http://www.php.net/manual/en/mysqli.query.php

mysqli is a base class of PHP and using it is object oriented.
There is both an object oriented syntax for usage and a procedural, but both use a mysqli class instance.

This means you just need a slight modification of procedural code from mysql_... to mysqli_... and already stop using the deprecated mysql_query() (and other mysql module functions). This gives you a way to reuse much code with just slight modifications.

If you want to go the full way then you'd change to mysqli or PDO_MySQL and use oop code syntax, too.

So in short:
The deprecation of mysql_query (and other mysql-functions) does not deprecate procedural programming style or syntax, as you can use the sister functions mysqli_...

OOP programming style is to be prefered, and you'd start by inheriting mysqli base class to your own needs data access class.

And last not least important: mysql_fetch_object() does not make PHP work object orientated, the results are only fetched in the form of simple record objects, with table fields as their properties/fields, but those objects are not instanciated classes with methods, events, etc.

Depending on how many code you have it's easier to migrate to mysqli, because of the procedural syntax you can continue to use, but I'd only make that decision, if the amount of code to rewrite would be a big issue.

See if mysqli or PDO make more sense in your case, by starting here:
http://www.php.net/manual/en/mysqlinfo.api.choosing.php

Bye, Olaf.
0
 

Author Comment

by:rgb192
ID: 39188536
>> then to be written in a common class and call the methods etc.,
does mysql class do this


>>mysqli
so mysql is not oo
0
 
LVL 29

Accepted Solution

by:
Olaf Doschke earned 500 total points
ID: 39189422
The short answer: Your code will not work in PHP 5.5.0 and later, you have to change it. But that is not, because it is procedural programming style.

The original MySQL extension of PHP is just a set of functions. And this set of functions simply is removed from PHP since 5.5.0

Other  PHP functions are still part of the language and of course you can use them within any code, no matter if procedural or class code. So if you program procedural or OOP doesn't matter.

What matters is, you are forced to use either mysqli or PDO, if you use PHP 5.5 or later.

The nice thing about mysqli is, you can use it both ways in procedural style or oop style, as described here: http://www.php.net/manual/en/mysqli.quickstart.dual-interface.php
And in procedural style the calls mostly differ from the old mysql-function by the additional i in mysqli and secondary, the new parameter called "link".

If you really want to benefit from OOP programming, then redo everything from scratch, don't just make the minor step to use mysqli methods in the procedural style. But if time is of the essence, you have the chance to move to mysqli via that procedural style, because of how similar the usage is.

Bye, Olaf.
0
 

Author Closing Comment

by:rgb192
ID: 39190001
most complete explanation, thanks
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MySQL - search xml field for multiple occurrences of specific text 6 62
Php  POSTGRES  "  IN SIDE "  " 4 77
PHP loop not working 4 62
Mysql Left Join Case 10 55
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question