ConnectDatabase

Aus Programmers Guide

(Unterschied zwischen Versionen)
Wechseln zu: Navigation, Suche
K
Zeile 35: Zeile 35:
== Connection als Klasse  ==
== Connection als Klasse  ==
 +
<source lang="php">
 +
require_once('MysqlConnector.php');
 +
 +
$mysqlConnector = new MysqlConnector();
 +
$mysqlConnector->PushData($server, $database, $username, $password);
 +
$mysqlConnector->Open();
 +
$result=$mysqlConnector->Query("SELECT * FROM Tabellenname WHERE irgendwas IS NULL;");
 +
$mysqlConnector->Close();
 +
 +
</source>
 +
 +
 +
'''MysqlConnector.php'''  
'''MysqlConnector.php'''  
Zeile 40: Zeile 53:
<source lang="php">
<source lang="php">
<?php
<?php
-
class Connection
+
class MysqlConnector
       {
       {
         var $db_connection = null;        // Database connection string
         var $db_connection = null;        // Database connection string
Zeile 51: Zeile 64:
         /** NewConnection Method
         /** NewConnection Method
         * This method establishes a new connection to the database. */
         * This method establishes a new connection to the database. */
-
         public function NewConnection($server, $database, $username, $password)
+
         public function PushData($server, $database, $username, $password)
         {
         {
             // Assign variables
             // Assign variables
Zeile 59: Zeile 72:
             $db_username = $username;
             $db_username = $username;
             $db_password = $password;
             $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();
 
-
            }
 
         }
         }
Zeile 140: Zeile 132:
                 echo 'Could not run query: '.$query ."<br>\n". mysql_error();
                 echo 'Could not run query: '.$query ."<br>\n". mysql_error();
                 exit;
                 exit;
 +
              }else{
 +
                return $result;
               }
               }
             }else
             }else
Zeile 145: Zeile 139:
                 return "Error: No connection has been established to the database. Cannot query.";
                 return "Error: No connection has been established to the database. Cannot query.";
             }
             }
 +
         }  
         }  

Version vom 06:23, 6. Apr. 2010

Standard-Connection

<?php
$host="localhost";
$user="";
$password="";
$db="";
 
$verbindung = mysql_connect ($host,$user, $password) or die ("keine Verbindung zur Datenbank moeglich.");
 
mysql_select_db($db) or die ("Die Datenbank existiert nicht.");
?>

Query-Function
<?php
function db_query($query){
	global $db_queries, $db_conn;
 
 
	$result=mysql_query($query);
	$db_queries++;
	if(mysql_error()){
		echo "<b>".$query."</b><br>\n".mysql_error();
		$result=null;
	}
 
return $result;
}
?>

Connection als Klasse

require_once('MysqlConnector.php');
 
$mysqlConnector = new MysqlConnector();
$mysqlConnector->PushData($server, $database, $username, $password);
$mysqlConnector->Open();
$result=$mysqlConnector->Query("SELECT * FROM Tabellenname WHERE irgendwas IS NULL;");
$mysqlConnector->Close();



MysqlConnector.php

<?php
class MysqlConnector
      {
        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 PushData($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;
        }
 
        /** 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.";
            }
         }
 
         /** Query Method
          * This method queries the Database with a given MySQL-String*/
         public function Query($query)
         {
            global $CONNECTED;
            if ($CONNECTED)
            {          
              $result = mysql_query($query);
              if (!$result) {
                echo 'Could not run query: '.$query ."<br>\n". mysql_error();
                exit;
              }else{
                return $result;
              }
            }else
            {
                return "Error: No connection has been established to the database. Cannot query.";
            }
 
         } 
 
}
?>
Persönliche Werkzeuge