Solved

is this procedural or object oriented

Posted on 2013-05-21
7
292 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 29

Expert Comment

by:Olaf Doschke
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
most complete explanation, thanks
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

More Fun with XML and MySQL – Parsing Delimited String with a Single SQL Statement Are you ready for another of my SQL tidbits?  Hopefully so, as in this adventure, I will be covering a topic that comes up a lot which is parsing a comma (or other…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now