note 92601 added to function.mysql-connect
| From: | sedativchunk at gmail dot com | Date: | Fri, 31 Jul 2009 04:36:24 +0000 |
| Subject: | note 92601 added to function.mysql-connect | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-158936@lists.php.net to get a copy of this message | ||
Here is a connection class I created for one of my websites for a MYSQL connection. Feel free to use
it on one of your applications anyone! It's MYSQL with OOP in mind and works similar to ASP.net
methods for connecting to a database. The benefit of this class is easy management of multiple MYSQL
connections.
The class:
<?php
class Connection
{
var $db_connection = null; // Database connection string
var $db_server = null; // Database server
var $db_database = null; // The database being connected to
var $db_username = null; // The database username
var $db_password = null; // The database password
var $CONNECTED = false; // Determines if connection is established
/** NewConnection Method
* This method establishes a new connection to the database. */
public function NewConnection($server, $database, $username, $password)
{
// Assign variables
global $db_connection, $db_server, $db_database, $db_username, $db_password;
$db_server = $server;
$db_database = $database;
$db_username = $username;
$db_password = $password;
// Attempt connection
try
{
// Create connection to MYSQL database
// Fourth true parameter will allow for multiple connections to be made
$db_connection = mysql_connect ($server, $username, $password, true);
mysql_select_db ($database);
if (!$db_connection)
{
throw new Exception('MySQL Connection Database Error: ' .
mysql_error());
}
else
{
$CONNECTED = true;
}
}
catch (Exception $e)
{
echo $e->getMessage();
}
}
/** Open Method
* This method opens the database connection (only call if closed!) */
public function Open()
{
global $db_connection, $db_server, $db_database, $db_username, $db_password, $CONNECTED;
if (!$CONNECTED)
{
try
{
$db_connection = mysql_connect ($db_server, $db_username, $db_password);
mysql_select_db ($db_database);
if (!$db_connection)
{
throw new Exception('MySQL Connection Database Error: ' .
mysql_error());
}
else
{
$CONNECTED = true;
}
}
catch (Exception $e)
{
echo $e->GetMessage();
}
}
else
{
return "Error: No connection has been established to the database. Cannot open
connection.";
}
}
/** Close Method
* This method closes the connection to the MySQL Database */
public function Close()
{
global $db_connection, $CONNECTED;
if ($CONNECTED)
{
mysql_close($db_connection);
$CONNECTED = false;
}
else
{
return "Error: No connection has been established to the database. Cannot close
connection.";
}
}
}
?>
----
Server IP: 64.71.164.2
Probable Submitter: 67.163.248.254
----
Manual Page -- http://www.php.net/manual/en/function.mysql-connect.php
Edit -- https://master.php.net/note/edit/92601
Del: integrated -- https://master.php.net/note/delete/92601/integrated
Del: useless -- https://master.php.net/note/delete/92601/useless
Del: bad code -- https://master.php.net/note/delete/92601/bad+code
Del: spam -- https://master.php.net/note/delete/92601/spam
Del: non-english -- https://master.php.net/note/delete/92601/non-english
Del: in docs -- https://master.php.net/note/delete/92601/in+docs
Del: other reasons-- https://master.php.net/note/delete/92601
Reject -- https://master.php.net/note/reject/92601
Search -- https://master.php.net/manage/user-notes.php