note 61356 added to function.mysql-connect
| From: | ed | Date: | Wed, 01 Feb 2006 14:46:56 +0000 |
| Subject: | note 61356 added to function.mysql-connect | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-102813@lists.php.net to get a copy of this message | ||
Personally I tend to use the following class for all my MySQL/PHP projects. As yet I've failed
to find a server where it doesn't work. Please note the class only works in PHP 5 due to the
'private' $var's inside the class.
<?php
class database {
private $ho;
private $dn;
private $du;
private $up;
/*
* $ho = Host
* $dn = Database Name
* $du = Database User
* $up = User Password
*/
public function connectionparameters($ho, $dn, $du, $up) {
// Sets the connection information
$this->ho = $ho;
$this->dn = $dn;
$this->du = $du;
$this->up = $up;
}
public function connect() {
// Establishes a mysql connection and selects the database
if(!$this->connection = mysql_connect($this->ho, $this->du, $this->up)) {
throw new exception('Connection to mysql server could not be established.');
}
if(!mysql_select_db($this->dn,$this->connection)){
throw new exception('Database could not be selected');
}
}
public function disconnect() {
// Disconnects
return mysql_close($this->connection);
}
}
?>
To establish a connection with this class:
<?php
$connection = new database;
$connection->connectionparameters('MYSQL_HOST', 'MYSQL_DB',
'MYSQL_USER', 'MYSQL_PASS');
try {
$connection->connect();
} catch (Exception $e) {
echo $e->getMessage();
exit;
}
?>
----
Server IP: 84.18.202.180
Probable Submitter: 83.104.108.216
----
Manual Page -- http://www.php.net/manual/en/function.mysql-connect.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+61356
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61356&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61356&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61356&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61356&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61356&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61356&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61356&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+61356&report=yes
Search -- http://master.php.net/manage/user-notes.php