By reading on stackoverflow I solved almost all of my problems. But now I’ve got a very “special” one I can’t explain. Hours of googling and researching, but there is no solution.
class:
class Beitrag_Loeschen {
private $debug;
private $do_debug;
function __construct() {
$this->do_debug = TRUE;
}
function __destruct() {
if ($this->do_debug == TRUE) {
mysql_query("INSERT INTO `seite_log` (`id`, `timestamp`, `benutzer_ip`, `benutzer_id`, `datei`, `referrer`, `fehler`, `kommentar`) VALUES (NULL, UNIX_TIMESTAMP(), '', 1, '', '', '', '')");
}
}
private function _deleteDir($dir) {
// more code
}
// SQL-Query
private function _queryDelete($tabelle, $spalte, $id) {
$query = "DELETE FROM `".sql($tabelle)."` WHERE `".sql($spalte)."`='".sql($id)."';";
mysql_query($query);
$this->debug .= $query."<br/>";
}
private function _queryDeleteKat($tabelle, $kat, $kat_id) {
$query = "DELETE FROM `".sql($tabelle)."` WHERE `kat`='".sql($kat)."' AND `kat_id`='".sql($kat_id)."'";
mysql_query($query);
$this->debug .= $query."<br/>";
}
private function _seiteSuchbegriffe($ergebnis_typ, $ergebnis_id) {
$query = "DELETE FROM `seite_suchbegriffe` WHERE `ergebnis_id`='".sql($ergebnis_id)."' AND `ergebnis_typ`='".sql($ergebnis_typ)."'";
mysql_query($query);
$this->debug .= $query."<br/>";
}
// Kommentare
function bewertungKommentar($id) {
$this->_queryDelete('bewertungen_kommentare', 'id', $id); // bewertungen_kommentare
}
function fotoKommentar($id) {
$this->_queryDelete('fotos_kommentare', 'id', $id); // fotos_kommentare
}
function rezeptKommentar($id) {
$this->_queryDelete('rezepte_kommentare', 'id', $id); // rezepte_kommentare
}
// Bewertung
function bewertung($id) {
$this->_queryDelete('bewertungen_burger', 'id', $id); // bewertungen_burger
$this->_queryDelete('bewertungen_kommentare', 'bewertung_id', $id); // bewertungen_kommentare
// more code
}
// Foto
function foto($id) {
$this->_queryDelete('fotos_kommentare', 'foto_id', $id); // fotos_kommentare
// more code
}
// Burger
function burger($id) {
$this->_queryDelete('burger', 'id', $id); // burger
$this->_queryDeleteKat('benutzer_aktionen', 'favorit_burger', $id); // benutzer_aktionen
// more code
}
// Lokalität
function lokalitaet($id) {
$this->_queryDeleteKat('seite_korrekturen', 'lokalitaet', $id); // seite_korrekturen
$this->_queryDeleteKat('seite_hits', 'lokalitaeten', $id); // seite_hits
// more code
}
}
My concept: If you want to, e. g., delete a location you have to delete rows from several tables. For this purpose I wrote this little PHP class. While deleting the stuff there are performed many mysql queries in many tables and therefore I needed a way to debug.
The debug information are stored in $debug.
$this->debug .= $query."<br/>";
Finally, $debug should stored in the database (after calling the class there is a “header(“Location: …”)”, so I could not perform an “echo”). The DB query for this is called in the destructor (INFO: the query is only a dummy. So I could obviate that there is no problem with the query-syntax):
function __destruct() {
if ($this->do_debug == TRUE) {
mysql_query("INSERT INTO `seite_log` (`id`, `timestamp`, `benutzer_ip`, `benutzer_id`, `datei`, `referrer`, `fehler`, `kommentar`) VALUES (NULL, UNIX_TIMESTAMP(), '', 1, '', '', '', '')");
}
}
The class is called by this:
$del = new Beitrag_Loeschen();
$del->bewertungKommentar($id);
//header("Location: ".$referer);
THE PROBLEM:
There seems to be no db connection within the destructor, because I got this error all the time:
Warning: mysql_query() [function.mysql-query]: Access denied for user ''@'localhost' (using password: NO) in /[...]/classes/beitrag_loeschen.inc.php on line 12
Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /[...]/classes/beitrag_loeschen.inc.php on line 12
(line 12 is the mysql_query in the destructor)
What am I doing wrong? The other mysql_querys in the class are working perfectly.
Thank you!
Unless you are explicitly calling the
__destruct()function or handling your references to the object very closely and know exactly when it gets unset there is no guarantee when__destruct()will be called by the language. By the time it is called the language has already released the resource for your connection to the database.mysql_query()attempts to create a new connection, using the default server parameters and fails to do so because there is no blank user.To reliably log your information you need to explicitly make the call to run the query. If you rely on
__destruct()for this you will likely always encounter some problem similar to the one you’re having.