note 56375 added to ref.mysqli

From: Date: Wed, 31 Aug 2005 23:03:53 +0000
Subject: note 56375 added to ref.mysqli
Groups: php.notes 
Request: Send a blank email to php-notes+get-94553@lists.php.net to get a copy of this message
I made a simple class that extends mysqli. It has the following features: - Use an XML file to store multiple connection parameters. - Auto display DB error messages (you can easily disable this in a production server) - Retrieve columns max lenght - Retrieve column comments - Retrieve ENUM|SET possible values as an array You are free to extend/modify it, but please give me credit. <?php class mysqli2 extends mysqli { private $dbHost, $dbUser, $dbPassword, $dbSchema; public function __construct($XMLFilePath, $connectionID) { $this->_getDBParameters($XMLFilePath, $connectionID); @parent::__construct($this->dbHost, $this->dbUser, $this->dbPassword, $this->dbSchema); if (mysqli_connect_errno()) { die(sprintf("Can't connect to database. Error: %s", mysqli_connect_error())); } } public function __destruct() { if (!mysqli_connect_errno()) { $this->close(); } } private function _getDBParameters($XMLFilePath, $connectionID) { if (!(file_exists($XMLFilePath))) { return FALSE; } $connections = simplexml_load_file($XMLFilePath); foreach ($connections as $connection) { if ($connection['ID'] == $connectionID) { $this->dbHost = $connection->dbHost; $this->dbUser = $connection->dbUser; $this->dbPassword = $connection->dbPassword; $this->dbSchema = $connection->dbSchema; } } } public function query($sql) { if (!($result = parent::query($sql))) { die(sprintf("Can't perform query on database. Error: %s", $this->error)); } return $result; } public function get_columns_max_len($dbTable) { if (!($result = $this->query("DESCRIBE $dbTable"))) { return FALSE; } while ($row = $result->fetch_array(MYSQLI_ASSOC)) { if (ereg('^.+\((.+)\)', $row['Type'], $columnMaxLen)) { $columnMaxLens[$row['Field']] = (int)$columnMaxLen[1]; } } return $columnMaxLens; } public function get_columns_comments($dbTable) { if (!($result = $this->query("SHOW CREATE TABLE $dbTable"))) { return FALSE; } $row = $result->fetch_row(); $tableSQL = explode("\n", $row[1]); foreach ($tableSQL as $tableSQLLine) { if (ereg(".+ (.+) .+ COMMENT '(.+)'", $tableSQLLine, $columnCommentsBuffer)) { $columnComments[$columnCommentsBuffer[1]] = $columnCommentsBuffer[2]; } } return $columnComments; } public function get_enum_options($dbTable, $dbColumn) { if (!($result = $this->query("DESCRIBE $dbTable"))) { return FALSE; } while ($row = $result->fetch_array(MYSQLI_ASSOC)) { if ($row['Field'] == $dbColumn) { if (eregi('(enum|set)\((.+)\)', $row['Type'], $enumValues)) { $enumValues = explode(",", str_replace("'", NULL, $enumValues[2])); } } } return isset($enumValues) ? $enumValues : FALSE; } } ?> The XML file looks like: <?xml version ='1.0' encoding ='UTF-8' ?> <connections> <connection ID="1"> <dbHost>*DATA*</dbHost> <dbUser>*DATA*</dbUser> <dbPassword>*DATA*</dbPassword> <dbSchema>*DATA*</dbSchema> </connection> </connections> ---- Manual Page -- http://www.php.net/manual/en/ref.mysqli.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+56375 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56375&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56375&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56375&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56375&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56375&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56375&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+56375&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#94553) next »