8516b95922f8510fc2994de60ca40e57ebbb2892
[acontent.git] / docs / include / classes / DAO / DAO.class.php
1 <?php
2 /************************************************************************/
3 /* AContent                                                             */
4 /************************************************************************/
5 /* Copyright (c) 2010                                                   */
6 /* Inclusive Design Institute                                           */
7 /*                                                                      */
8 /* This program is free software. You can redistribute it and/or        */
9 /* modify it under the terms of the GNU General Public License          */
10 /* as published by the Free Software Foundation.                        */
11 /************************************************************************/
12
13 /**
14 * Root data access object
15 * Each table has a DAO class, all inherits from this class
16 * @access       public
17 * @author       Cindy Qi Li
18 * @package      DAO
19 */
20
21 class DAO {
22
23         // private
24         static private $db;     // global database connection
25         
26         function DAO()
27         {
28                 if (!isset($this->db))
29                 {
30                         $this->db = @mysql_connect(DB_HOST . ':' . DB_PORT, DB_USER, DB_PASSWORD);
31                         if (!$this->db) {
32                                 die('Unable to connect to db.');
33                         }
34                         if (!@mysql_select_db(DB_NAME, $this->db)) {
35                                 die('DB connection established, but database "'.DB_NAME.'" cannot be selected.');
36                         }
37                 }
38         }
39         
40         /**
41         * Execute SQL
42         * @access  protected
43         * @param   $sql : SQL statment to be executed
44         * @return  $rows: for 'select' sql, return retrived rows, 
45         *          true:  for non-select sql
46         *          false: if fail
47         * @author  Cindy Qi Li
48         */
49         function execute($sql)
50         {
51                 $sql = trim($sql);
52                 $result = mysql_query($sql, $this->db) or die($sql . "<br />". mysql_error());
53
54                 // for 'select' SQL, return retrieved rows
55                 if (strtolower(substr($sql, 0, 6)) == 'select' && mysql_num_rows($result) > 0) 
56                 {
57                         for($i = 0; $i < mysql_num_rows($result); $i++) 
58                         {
59                                 $rows[] = mysql_fetch_assoc($result);
60                         }
61                         mysql_free_result($result);
62                         return $rows;
63                 }
64                 else
65                         return true;
66         }
67
68 }
69 ?>