moved code up one level to eliminate the docs subdirectory
[acontent.git] / 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') 
56                 {
57                         if (mysql_num_rows($result) > 0) {
58                                 for($i = 0; $i < mysql_num_rows($result); $i++) 
59                                 {
60                                         $rows[] = mysql_fetch_assoc($result);
61                                 }
62                                 mysql_free_result($result);
63                                 return $rows;
64                         } else {
65                                 return false;
66                         }
67                 }
68                 else {
69                         return true;
70                 }
71         }
72
73 }
74 ?>