na insbesondere ist $verbindung jeweils eine lokale variable:
variante 1: (unschön)
PHP-Code:
function db_connect() {
global $verbindung, $mysql_db;
include("config.php");
$verbindung = mysql_connect($mysql_host,$mysql_user,$mysql_pass);
if(!$verbindung)die("Keine Verbindung zur Datnbank!");
}
function db_close() {
global $verbindung;
mysql_close($verbindung);
}
function access_log_list() {
global $verbindung, $mysql_db;
db_connect();
$abfrage="select * from access_log";
$access_list=mysql_db_query($mysql_db, $abfrage, $verbindung);
...
}
variante 2 (besser)
PHP-Code:
<?php
class DataBase {
protected $verbindung;
protected $mysql_db;
function __construct() {
$this->db_connect();
}
function __destruct() {
$this->db_close();
}
function db_connect() {
if ($this->verbindung) return; // verbindung besteht bereits
include("config.php");
$this->verbindung = mysql_connect($mysql_host,$mysql_user,$mysql_pass);
if(!$this->verbindung)die("Keine Verbindung zur Datnbank!");
$this->mysql_db = $mysql_db; // funzt aber nur wenn $mysql_db in config.php definiert wird!
}
function db_close() {
if ($this->verbindung) {
mysql_close($this->verbindung);
$this->verbindung = null;
}
}
function access_log_list() {
$abfrage="select * from access_log";
$access_list=mysql_db_query($this->mysql_db, $abfrage, $this->verbindung);
// ...
}
} // class DataBase
$db = new DataBase();
$db->access_log_list();
variante 3, am besten:
http://adodb.sourceforge.net/