Subversion Repositories oidplus

Rev

Rev 236 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 236 Rev 237
Line 80... Line 80...
80
                }
80
                }
81
        }
81
        }
82
 
82
 
83
        public function insert_id(): int {
83
        public function insert_id(): int {
84
                try {
84
                try {
85
                        $res = $this->query("SELECT LAST_INSERT_ID AS ID"); // MySQL
85
                        $res = $this->query("SELECT LAST_INSERT_ID() AS ID"); // MySQL
86
                } catch (Exception $e) {
86
                } catch (Exception $e) {
87
                        $res = null;
-
 
88
                }
-
 
89
 
-
 
90
                try {
87
                        try {
-
 
88
                                $res = $this->query("SELECT SCOPE_IDENTITY() AS ID"); // Microsoft SQL Server
-
 
89
                        } catch (Exception $e) {
-
 
90
                                try {
91
                        if (!$res) $res = $this->query("SELECT @@IDENTITY AS ID"); // MS SQL
91
                                        $res = $this->query("SELECT LASTVAL() AS ID"); // PostgreSQL
92
                } catch (Exception $e) {
92
                                } catch (Exception $e) {
93
                        $res = null;
93
                                        $res = null;
-
 
94
                                        // return 0;
-
 
95
                                        throw new Exception("Cannot determine the last inserted ID. The DBMS is probably not supported.");
-
 
96
                                }
-
 
97
                        }
94
                }
98
                }
95
 
-
 
96
                if (!$res) return 0;
-
 
97
 
99
 
98
                $row = $res->fetch_array();
100
                $row = $res->fetch_array();
99
                return (int)$row['ID'];
101
                return (int)$row['ID'];
100
        }
102
        }
101
 
103
 
102
        public function error(): string {
104
        public function error(): string {
103
                return odbc_errormsg($this->odbc);
105
                return odbc_errormsg($this->odbc);
104
        }
106
        }
105
 
107
 
106
        private $html = null;
-
 
107
        public function init($html = true): void {
-
 
108
                $this->html = $html;
-
 
109
        }
-
 
110
 
-
 
111
        public function connect(): void {
108
        public function connect(): void {
112
                // Try connecting to the database
109
                // Try connecting to the database
113
                $this->odbc = @odbc_connect(OIDPLUS_ODBC_DSN, OIDPLUS_ODBC_USERNAME, base64_decode(OIDPLUS_ODBC_PASSWORD));
110
                $this->odbc = @odbc_connect(OIDPLUS_ODBC_DSN, OIDPLUS_ODBC_USERNAME, base64_decode(OIDPLUS_ODBC_PASSWORD));
114
 
111
 
115
                if (!$this->odbc) {
112
                if (!$this->odbc) {
116
                        if ($this->html) {
-
 
117
                                echo "<h1>Error</h1><p>Database connection failed! (".odbc_errormsg().")</p>";
-
 
118
                                if (is_dir(__DIR__.'/../../../setup')) {
-
 
119
                                        echo '<p>If you believe that the login credentials are wrong, please run <a href="setup/">setup</a> again.</p>';
-
 
120
                                }
-
 
121
                        } else {
-
 
122
                                echo "Error: Database connection failed! (".odbc_errormsg().")";
113
                        parent::showConnectError(odbc_errormsg());
123
                                if (is_dir(__DIR__.'/../../../setup')) {
-
 
124
                                        echo ' If you believe that the login credentials are wrong, please run setup again.';
-
 
125
                                }
-
 
126
                        }
-
 
127
                        die();
114
                        die();
128
                }
115
                }
129
 
116
 
130
                try {
117
                try {
131
                        $this->query("SET NAMES 'utf8'"); // Does most likely NOT work with ODBC. Try adding ";CHARSET=UTF8" (or similar) to the DSN
118
                        $this->query("SET NAMES 'utf8'"); // Does most likely NOT work with ODBC. Try adding ";CHARSET=UTF8" (or similar) to the DSN
132
                } catch (Exception $e) {
119
                } catch (Exception $e) {
133
                }
120
                }
134
                $this->afterConnect($this->html);
121
                $this->afterConnect();
135
                $this->connected = true;
122
                $this->connected = true;
136
        }
123
        }
137
 
124
 
138
        private $intransaction = false;
125
        private $intransaction = false;
139
 
126