• 0

[php] oop tutorials with mysql


Classic or oop php?  

12 members have voted

  1. 1. Classic or oop php?

    • Classic
      6
    • oop
      6


Question

Hey,

I'm trying to learn php oop. I know some php, but adding in oop is a little confusing. I got the basics, but I need a simple tutorial that deals with mysql. connecting, selecting, inputting. I can't seem to find any good tutorials...

Thanks for any help :)

And I included a poll, just a little curious what the results will be.

Do you program using classic php (What I've heard it referred to a few times, not sure if thats the 'official' name or not) or oop?

Link to comment
https://www.neowin.net/forum/topic/865494-php-oop-tutorials-with-mysql/
Share on other sites

8 answers to this question

Recommended Posts

  • 0

What about some good books for oop php and mysqli? Ive found it surprisingly hard to found a good tutorial...I have a gift card for Chapters, so can pick up a book, just not sure which one everyone prefers or can recommend.

Thanks for any help :)

  • 0

"Classic PHP" as you call it is "officially" procedural coding.

I'm not sure what you are after? General MySQL tutorials or something specific to using MySQL in an object-oriented environment? If the former then there are loads of tutorials such as the one Andrew posted above. MySQL is used the same in procedural or object oriented coding methods.

If the latter then there is no specific or standard way that I know. You could simply have a separate object for handling database queries (typically referred to as a model) with methods for performing specific actions. This is how I tend to do it so that the database is separate from the logic, but not too abstract. A quick example:

class MyPostDatabase
  extends Database
{
  public function selectPosts($start, $limit)
  {
	return $this->query("SELECT title, body FROM posts LIMIT $start, $limit");
  }
}

$db = new MyPostDatabase;
$res = $db->selectPosts(0, 10);

The database class it extends contains a method called query, which performs the query using the many PHP MySQL functions and returns the result as an associative array (eg: $res['title']).

Or you can treat the whole database/table/query as an object. An example of this can be found in the Zend framework. I personally don't like this method as it complicates things unnecessarily in my opinion as it basically rewrites the whole MySQL language in PHP. It seems pointless to abstract things so much to me.

If you have more specific questions then there are plenty of people here that can help. :)

EDIT: This is the site I used when first starting out: http://www.php-mysql-tutorial.com/wikis/my...l-database.aspx

Edited by Fourjays
  • 0

You're going about this the wrong way. You're confused because you don't understand the concepts related to object-oriented programming.

Forget PHP and MySQL for a second. Read up on some OOP fundamentals then figure out how to apply those techniques using the language of your choice. Your code will be pretty awful at first, but you'll realize what was painful, and what worked. It's a process. Don't expect flawless results overnight.

  • 0

What I was looking for was in Fourjays' example. I'm trying to learn oop, and so wanted to do a small project, to help me understand the basics, but I find anything without a database, and dynamic boring.

I do know mysql, and php, but making it all an object, I found rather hard as I couldn't find any good tutorials. Trying to keep the php away from the html is tricky when having done it the other way for so long.

I found a script here, a dog example, that was great, but it didn't show how to show all your results. I believe that Fourdays' example has shown me how to do that, and has helped get me a little farther in my project.

sbauer - I believe you are very right about that. I think sometimes I try to get too far ahead of myself.

Thank you for the help :)

  • 0

As others have suggested, OOP and MySQL aren't something you learn together. Procedural (or what you call "classic") PHP consumes well written OOP PHP (the way I see it). OOP is just another programming tool to have. I like OOP because well written OOP code is easier to maintain, document, and extend the functionality of. OOP code can be difficult for beginners, because you have to think about your implementations in a different way.

If you don't have any web programming experience, start with simple procedural PHP and get the hang of programming for server side applications.

A good book I used for OOP in PHP is:

PHP 5: Objects, Patterns, and Practice

Kind of an old book, now, but I still refer to it sometimes. Another good resource to have for any OOP developer is Design Patterns. Elements of Reusable Object-Oriented Code.

Edit:

  Quote
I do know mysql, and php, but making it all an object

As an example of the power of OOP, you would not code a site such that it was a MySQL site. You would code a site such that it consumes some data from some database. You would implement abstract classes that define the interface you need to consume data from some data source. Then you would develop child classes that contain the specifics of the MySQL interactions to feed to your main program. This way, if you decide to change your mind and don't want to use MySQL anymore, you can develop another child class that defines the relationship between your interface and another data source. Then there is no need to change your main program that defines your application. Just swap classes.

If you jump in it like all your classes are going to be using MySQL, then you have missed a fundamental OOP concept of the abstract interface.

Edited by Shadrack
  • 0
  On 19/01/2010 at 14:03, Fourjays said:

--x--snip--x--

Or you can treat the whole database/table/query as an object. An example of this can be found in the Zend framework. I personally don't like this method as it complicates things unnecessarily in my opinion as it basically rewrites the whole MySQL language in PHP. It seems pointless to abstract things so much to me.

--x--snip--x--

Depending on the application, it can be completely pointless (or unnecessarily time consuming), or mission critical. Developers have to understand the application they are working on, know what tools they have and use their brains :).

It doesn't rewrite the whole MySQL language. It abstracts it so that (if you use it) adapting your code to other data sources is trivial. If you have ever had to convert code from one data source to another one, you will see that it is not pointless (for every project). But if you choose not to use abstraction on the data end, there is nothing to say you can't use abstraction on some other feature in your code.

I guess the points I'm trying to make are:

* Knowing OOP is a good thing

* OOP isn't necessary for every project

* Leveraging OOP's usability depends on the size and scope of a project. The larger a project is the better it will be if you implement OOP code.

* If I'm putting together something simple, that I don't plan to extend, I don't use OOP.

Edited by Shadrack
  • 0

Shadrack - Thank you very much :) Your messages are very helpful, and I think I do understand what you are talking about.

I do know some Procedural php, but thought it was time that I expanded on what I know.

I will take a look at those books as well :)

Thank you very much for your help :)

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • AMD releases new Windows 10 and 11 chipset driver for Ryzen processors by Taras Buria Following a major firmware update for a TPM-Pluton security flaw, AMD has a new chipset driver for all of its supported Ryzen platforms and processors. Version 7.06.02.123 is now available for download, and it adds unnamed bug fixes and an important addition to the AMD PPM Provisioning File driver, which improves the security of the component. The security update in question is CETCOMPAT support for the AMD PPM Provisioning File driver. CET or Control-flow Enforcement Technology (CET) was introduced for Windows 10 versions 20H1 and 20H2 in early 2021. In a nutshell, it is an Intel-developed protection system that prevents malware from changing memory addresses (Shadow Stack) or redirecting programs to malicious code by ensuring they use only approved, safe locations (Indirect Branch Tracking). AMD brought CETCOMPACT support to parts of its Ryzen chipset driver suite (the driver you download and install is a set of different drivers serving different purposes that AMD lets you select on the first screen) in October 2024. Now, with driver 7.06.02.123, CET compatibility has been added to the AMD PPM Provisioning File driver, which is responsible for core parking and power management. Here is the list of updated drivers in today's release: Driver Version (Windows 10) Version (Windows 11) Changelog AMD PPM Provisioning File Driver 8.0.0.53 8.0.0.53 CETCOMPAT support is added AMD PMF-6000 Series Driver 24.0.6.0 24.0.6.0 Bug fixes AMD S0i3 Filter Driver 4.5.0.1020 4.5.0.1020 AMD 3D V-Cache Performance Optimizer Driver 1.0.0.11 1.0.0.11 AMD PMF-7040 Series Driver 24.2.6.0 24.2.6.0 AMD PMF-8000Series Driver 25.5.25.0 25.5.25.0 AMD PMF Ryzen AI 300 Series Driver 1 Not Applicable 25.6.28.0 AMD's latest Ryzen chipset driver supports 64-bit Windows 10 and 11 systems with the following motherboards: A-Series B-Series X-Series Threadripper AMD A320 AMD A520 AMD B350 AMD B450 AMD B550 AMD B650 / B650E AMD X370 AMD X470 AMD X570 AMD X670 / X670E AMD X870 / X870E AMD TRX40 AMD TRX50 AMD WRX80 AMD WRX90 You can download AMD Ryzen Chipset Driver 7.0.6.02.123 from the official support website. Full release notes and additional information is available on the same page.
    • I never got to experience BOB Hello even now because gaming laptops I bought always omitted the IR camera. And the standalone cameras for BOB Hello are super expensive
    • I wonder what $99 Chinese cellphone this phone will be based on. Whoever is dumb enough to buy the phone and/or get this service deserves everything they get.
    • Trump is a low-class guy who thinks painting everything in gold makes him look fancy.
    • Don't forget second season pass, that's not part of the superdeluxe edition.
  • Recent Achievements

    • Explorer
      treker_ed went up a rank
      Explorer
    • Apprentice
      CHUNWEI went up a rank
      Apprentice
    • Veteran
      1337ish went up a rank
      Veteran
    • Rookie
      john.al went up a rank
      Rookie
    • Week One Done
      patrickft456 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      642
    2. 2
      ATLien_0
      274
    3. 3
      +FloatingFatMan
      174
    4. 4
      Michael Scrip
      156
    5. 5
      Steven P.
      138
  • Tell a friend

    Love Neowin? Tell a friend!