views:

61

answers:

3

I'm a bit new to OOP, but i've been playing with it for about a month now. Usually, i create a class called Mysql which has a __construct function that connects to a database directly. And after that i have lots of different functions that gets or inserts data into different tables.

On the bus home today, i began thinking and i came up with a brilliant idea that would make it less cluttered. My idea is to use one single function that selects data (and one for inserting), and depending on how the query that's passed in looks, it will select different data from different tables. Nice, right?

But i'm kind of stuck here. I'm not sure at all how to achieve this. I've got a small clue how it could work, but i don't know how i would bind the results, or fetch them into an array. The query will be created in another method, and then be passed into the select/insert function within the Mysql class.

I drew a "sketch" on how i think it may work. Here it is:

Of course, the function below will be placed in the Mysql class, and will already have connection to a database.

// This is an example query that could be passed in.
$query = "SELECT * FROM table WHERE id=150";

function select_data($query) {
    if ( $smtp = $this->conn->prepare($query) ) {

        $smtp->execute();
        $smtp->bind_results(What happens here?);

        if ( $smtp->fetch() ) {

            foreach ( fetched_row? as $key => $value ) {
                $return[] = $key => $value;
            }

            return $return;

        }
        else return NULL;

    }
    else return $this->conn->error;
}

Thanks a lot to anyone who can show me how this can be achieved.

A: 

I think you are running into problems when you need related data. In other words, when an object of yours has a property which is another object that data should also be gathered and dynamically filled. I once came pretty far but when stuff like INNER, LEFT and RIGHT joins come accross you'll think twice about going further ;)

About bind_results:http://php.net/manual/en/mysqli-stmt.bind-result.php

(Maybe slightly off topic; SMTP? That's a mailprotocol, are you sure you don't mean MySQLi's STMT?)

Ben Fransen
I wont be using joins for this, i think. But even if i do, how would that make it not work? The query will always be similar to the one mentioned in my first post, possibly throwing a join in if it works... Yeah, i meant STMT, not SMTP, hehe. Thanks!
Nike
A: 

For reference, PDO already does a lot of what you seem to want to do. It even has a fetchAll method that returns an array of rows, much like your function does. You don't need to bind anything in order to use it, unless you have parameters in your query string (and of course, values to bind to those parameters).

Check out the PDO documentation, and see if that doesn't fit your needs. Particularly PDOStatement->fetchAll().

cHao
+1  A: 

You have more options to use in PHP and they has their own specifics. I can recommend some ORM like Doctrine because of ease of use, stability, community and most importantly efectivity.

You can use it as easy as:

$q = Doctrine_Query::create()
  ->select('u.username, p.phone')
  ->from('User u')
  ->leftJoin('u.Phonenumbers p');

$users = $q->fetchArray();

or:

// Delete phonenumbers for user id = 5
$deleted = Doctrine_Query::create()
  ->delete()
  ->from('Phonenumber')
  ->andWhere('user_id = 5')
  ->execute();

// Make all usernames lowercase
Doctrine_Query::create()
  ->update('User u')
  ->set('u.username', 'LOWER(u.username)')
  ->execute();

// 'like' condition
$q = Doctrine_Query::create()
  ->from('User u')
  ->where('u.username LIKE ?', '%jwage%');

$users = $q->fetchArray();
palmic
I'll have to look into that a bit more, thanks for the reply! :)
Nike