Jump to content

Database Queries in OOP


Archadian

Recommended Posts

OOP is just a style of programming, in the case of someone doing $db->query() they're calling the function query() in the object that $db is an instance of ($db = new Object();).

 

There are many classes (objects) that have mysql functionality... PDO (works for most databases) and Mysqli being two off the top of my head.

Is there a way to use a function for all tables or will i have to make a function for each table?

 

Your question, unfortunately, shows a fundamental lack of programming knowledge. Jumping in to OO-Development with such a basic skill set will cause more harm than good, in my opinion. While it's always great to branch out and learn a diverse programming vocabulary, you should start with gaining some experience first.

 

To answer your question, it depends on what you're trying to accomplish. If you want to write a function which loads, say, session data related to a user, it could be categorized as 'have[ing] to make a function for each table'

 

However, the real question is, how are you executing this query?

 

Do you do it like this? (Functional)

<?php


$conn = mysql_connect("localhost", "user", "pw");
mysql_select_db("some_db");


function get_session_data($conn, $userid) { # $conn is the db connection we're using
    $q = mysql_query("select sessiondata from user", $conn);
    while($row=mysql_fetch_row($q)) {
        // do something with the result set
    }

    return $data

}
?>

 

Or like this? (Functional and a bit more structured)

<?php
function query($query, $conn) {
    //build an array of results.
    $ret = array(); # to be populated and returned

    $q = mysql_query($query, $conn);
    while($row=mysql_fetch_row($q)) {
        $ret[] = $row; //add to the $ret array.
    }

    return $ret

   
}

function get_session_data($user_id) {
   $res_array = query("select sessiondata from user where userid ='$user_id'");
   $data = $# get some data....

   return $data

}
?>

 

Or like this? (OOP)

 

<?php
class Database {
    private $connection;

    public function __contruct($host, $username, $password, $database) {
        $this->connection = mysql_connect($host, $username, $password);
        mysql_select_db($database, $connection);
    }

    public function query($q) {
    //build an array of results.
        $ret = array(); # to be populated and returned

        $q = mysql_query($query, $this->connection);
        while($row=mysql_fetch_row($q)) {
            $ret[] = $row; //add to the $ret array.
        }

        return $ret
    }

}

class User {
   private $db_conn;

   public function __construct() { $this->db_conn = new Database("localhost", "user", "pw", "some_db"); }

   public function get_session_data() {
       $this->db_conn->query("select sessiondata from user where userid = '$user_id'");
       // do something with the data..

       return $data
   }

}
?>

 

 

 

 

 

How do i gain experience if i don't ask questions like this? I thought gaining experience was learning. I know how to do the db connection i just thought there might be a better way or another way to use one function to connect to the tables i need to connect to. Thank you for the reply :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.