$val) { $this->$key = $val; } } /** * Connect to the server * * @return bool * @throws PDOException */ public function login() { $this->conn = new PDO('mysql:host=' . $this->host . ';port=' . $this->port . ';dbname=mysql', $this->user, $this->pass); return (is_object($this->conn)); } /** * Obtain a list of the databases visible on this server * when connected with the current credentials * * @return array */ public function getDatabases() { $q = $this->conn->query('SHOW DATABASES'); $ret = $q->fetchAll(PDO::FETCH_COLUMN, 0); $q->closeCursor(); return $ret; } /** * Return the status variables of the server, or a specific * variable if $key is not empty * * Note that if $key is not empty, there is no way to differentiate * between a NULL result and an undefined variable. This seems most * appropriate for "sloppy" code like PHP. * * @todo check whether it wouldn't be faster in the $key case to just fetch all like in the no key case, and use array indexing to extract the result * * @param string $key * @return mixed Return an array if $key is empty, or a single value if it is set */ public function getStatus($key = NULL) { $q = $this->conn->query('SHOW STATUS'); if (empty($key)) { $ret = $q->fetchAll(PDO::FETCH_COLUMN, 0); $q->closeCursor(); } else { $ret = NULL; while ($row = $q->fetch(PDO::FETCH_NUM)) { if ($row[0] === $key) { $ret = $row[1]; $q->closeCursor(); break; } } } return $ret; } /** * Flush the query cache, optionally emptying it. Flushing without * emptying just defragments the cache, losing nothing * * @param bool $reset * @return void */ public function flushQueryCache($reset = FALSE) { $sq = ($reset ? 'RESET' : 'FLUSH') . ' QUERY CACHE'; $ret = $this->conn->query($sq); $ret->closeCursor(); } }