conn = mysqli_init (); $conn->options (MYSQLI_READ_DEFAULT_FILE, __DIR__.'/my.cnf'); @$conn->real_connect ($host, $user, $pass, $name); if (mysqli_connect_errno ()) { sleep (1); throw new Exception (mysqli_connect_errno (), mysqli_connect_error ()); return FALSE; } $this->isOpen = TRUE; $this->query ('SET CHARACTER SET utf8'); return TRUE; } /** * Closes the current connection, if it's closed does nothing. **/ function close () { if ($this->isOpen) $this->conn->close (); $this->isOpen = FALSE; } /** * Checks whether the connection is open. * * @return boolean %TRUE if connection is open, %FALSE otherwise **/ function isOpen () { return $this->isOpen; } /** * Executes the query and gets it's first row. * * @param string $query The SQL query * @param mixed[] $params The query parameters * * @return mixed[] An associative array with the first row, %NULL if error **/ function getRow ($query, $params = NULL) { $result = $this->query ($query, $params); if ($result && ($row = $result->fetch_assoc ())) return $row; return NULL; } /** * Executes the query and gets the first value of the first row. * * @param string $query The SQL query * @param mixed[] $params The query parameters * * @return mixed The value or %NULL if error **/ function getValue ($query, $params = NULL) { $result = $this->query ($query, $params); if ($result && ($row = $result->fetch_row ())) return $row[0]; return NULL; } /** * Executes the query stored in the specified file and gets the result. * * @param string $query The SQL query * @param mixed[] $params The query parameters * * @return mixed The value or %NULL if error **/ function queryFromFile ($file, $params = NULL) { $query = file_get_contents ($file); if ($query) return $this->query ($query, $params); return NULL; } /** * Executes the query and gets the result. * * @param string $query The SQL query * @param mixed[] $params The query parameters * * @return mixed The value or %NULL if error **/ function query ($query, $params = NULL) { $result = $this->conn->query (Render::toString ($query, $params)); if (!$result) $this->checkError (); else while ($this->moreResults ()) $this->nextResult (); return $result; } /** * Execute multiple queries separated by semicolons. * * @param string $query The SQL query * @param mixed[] $params The query parameters * * @return mixed The value or %NULL if error **/ function multiQuery ($query, $params = NULL) { $success = $this->conn->multi_query (Render::toString ($query, $params)); if (!$success) $this->checkError (); return $success; } function storeResult () { $result = $this->conn->store_result (); if (!$result) $this->checkError (); return $result; } function moreResults () { return $this->conn->more_results (); } function nextResult () { return $this->conn->next_result (); } private function checkError () { if ($this->conn->errno) { $code = $this->conn->errno; $message = $this->conn->error; $sql = 'SELECT es_es message, 2000 + id AS id '. 'FROM sql_error WHERE code = @err'; if ($code == 1305 && ($row = $this->getRow ($sql))) throw new Exception ($row['id'], $row['message']); else throw new Exception ($code, $message); } } function checkWarnings () { if ($this->conn->warning_count > 0) if ($result = $this->conn->query ('SHOW WARNINGS')) { $warnings = []; $sql = 'SELECT es_es message, 3000 + id AS id '. 'FROM sql_warning WHERE code = @warn'; while ($row = $result->fetch_assoc ()) { if ($row['Code'] == 1265 && ($warning = $this->getRow ($sql))) { $row['Code'] = $warning['id']; $row['Message'] = $warning['message']; } $warnings[] = $row; } return $warnings; } return NULL; } } ?>