The right way to create a DAO in PHP

From an OOP point of view, how can I create a DAO (data access object) in php?

For example (using an account as a basic example), my AccountDAO will perform the following functions:

  • GetAllAccounts
  • GetAccountByID
  • UpdateAccount
  • DeleteAccount
  • InsertAccount

So, as soon as I get all the accounts in the database, should I return them to the caller as an array of account objects? Should I just return the mysql result set?

Do you know a good example of a DAO?

+5
source share
2 answers

You're on the right track to create a DAO: the methods you listed should definitely be part of the DAO.

, DAO - , , , .

, MySQL. , MySQL ( ).

. , , . .

0

, 3 . , , - , SQL Query

<?php
class Database{

    public function con_open(){
        $con = mysql_connect("domain","username","password");
            if (!$con)
            {
                die('Could not connect: ' . mysql_error());
            }
        mysql_select_db("db_name", $con);
    }

    public function query($sql){
        $result = mysql_query($sql);
            if(!$result){

            }else{      
                    return $result;
                }
    }

    public function fetch($result){
        $row = mysql_fetch_array($result);
        return $row;
    }

    public function con_close(){
         mysql_close($con);
    }
}


$db = new Database();

?>

, .

-1

All Articles