martenk
Goto Top

Probleme mit altem PHP Script und DB Anbindung

Hallo,

habe mal eine Frage bzgl. einer Fehlermeldung bei der Anbindung einer MYSQL Datenbank

ist ein etwas älteres Script, welches bei uns noch läuft auf einem alten Rechner - wollte nun mit dieser DB umziehen und habe mit zu testzewcken xampp installiert

habe die DB vom "alten" PC exportiert und auf dem "Neuen" importiert unter dem gleichen Namen und den Ordner "ts8" unter htdocs ebenfalls kopiert

Wenn ich nun auf dem neuen Rechner unter 127.0.0.1/ts8 die site aufrufen möchte, dann bekomme ich diese Meldung
Fatal error: Uncaught mysqli_sql_exception: Unknown database 'localhost' in C:\xampp\htdocs\ts9\classes\Core.php:62 Stack trace: #0 C:\xampp\htdocs\ts9\classes\Core.php(62): mysqli->select_db('localhost') #1 C:\xampp\htdocs\ts9\check.php(19): Core::connectDB() #2 {main} thrown in C:\xampp\htdocs\ts9\classes\Core.php on line 62  

das ist der code
<?php

/**
 * This is a simplified Core-Class. In this case UI-logic and db-bindings
 *  are combined as one.
 * @
 */

//define ( 'MYSQL_HOST',      'localhost' ); 
//define ( 'MYSQL_BENUTZER',  'root' ); 
//define ( 'MYSQL_KENNWORT',  '' ); 
//define ( 'MYSQL_DATENBANK', 'timescheduler' ); 

class Core {
    /* */

    private static $config_file = "conf.ini";  

    /* */
    //var $db_conf;

    private static $link = null;
    
    /**
     * 
     * @param type $file
     */
    public static function getDBConfig() {
        $conf = parse_ini_file(Core::$config_file);
        return $conf;
        //die(var_dump($conf) );
        //exit;
        //$db_conf = array();
        //$this->db_conf = $conf;
    }

    public static function connectDB() {
        $conf = Core::getDBConfig();
        //$link = mysql_connect($conf["host"], $conf["user"], $conf["password"]) or die("Could not connect: " . mysql_error()); 
/*$link = mysql_connect (MYSQL_HOST, 
                           MYSQL_BENUTZER, 
                           MYSQL_KENNWORT, 
                           MYSQL_DATENBANK);*/
        //Core::$link = new mysqli(MYSQL_HOST, MYSQL_BENUTZER, MYSQL_KENNWORT, MYSQL_DATENBANK);
        Core::$link = new mysqli($conf["host"], $conf["user"], $conf["password"], $conf["name"]);  
        //mysql_set_charset('utf8', $link); 
        /* check connection */
        if (mysqli_connect_errno()) {
            printf("Connect failed: %s\n", mysqli_connect_error());  
            exit();
        }
        if (!Core::$link->set_charset("utf8")) {  
            printf("Error loading character set utf8: %s\n", Core::$link->error);  
        } else {
            //printf("Current character set: %s\n", Core::$link->character_set_name()); 
        }
        
        // select our database
        
        // select our database
        //mysql_select_db(MYSQL_DATENBANK) or die(mysql_error());
        Core::$link->select_db($conf["host"]);  
    }

    public static function closeDB() {
        //mysql_close();
        Core::$link->close();
    }

    /**

das ist die ini Datei
host = "localhost"  
port = "3306"  
user = "root"  
password = ""  
name = "timescheduler"  

[Settings]
title="Terminplaner"  
time_granularity=5
day_begin_hour=8
day_end_hour=19

Content-Key: 6274446909

Url: https://administrator.de/contentid/6274446909

Printed on: April 27, 2024 at 06:04 o'clock

Mitglied: 6247018886
Solution 6247018886 Mar 08, 2023 updated at 13:55:51 (UTC)
Goto Top
Core::$link->select_db($conf["host"]);
Wenn man statt dem Tabellennamen den Hostnamen beim Select übergibt verwundert mich dir Fehlermeldung ehrlich gesagt nicht 😁. Die Fehlermeldung verrät dir übrigens auch schon die Zeile 😜

Fehlermeldung sind übrigens dazu da das man sie auch mal liest und nicht nur Copy n pasted 😉

Cheers briggs
Member: tagol01
tagol01 Mar 08, 2023 at 14:01:44 (UTC)
Goto Top
Und "user = "root"" sollte man sich sehr gut überlegen!
Member: aqui
aqui Mar 08, 2023 updated at 14:13:25 (UTC)
Goto Top
Mit einem
mysql -u root
und einem anschliessenden "SHOW DATABASES;" hätte ihm der Datenbank Server sicher auch gezeigt das das "Unknown database 'localhost'" berechtigt ist, da es sehr wahschreinlich gar keine Datenbank mit diesem Namen gibt. Zumal sie ja eigentlich "timescheduler" heisst?!
Member: martenk
martenk Mar 08, 2023 at 14:32:21 (UTC)
Goto Top
Prima - daran lag es - nun läuft die DB z.T. - bekomme nun bei einer Liste, die als PDF angezeigt werden soll, diese Meldung

Fatal error: Uncaught Error: Call to undefined function get_magic_quotes_runtime() in C:\xampp\htdocs\ts12\fpdf\fpdf.php:1043 Stack trace: #0 C:\xampp\htdocs\ts12\fpdf\fpdf.php(77): FPDF->_dochecks() #1 C:\xampp\htdocs\ts12\stafftopdf.php(39): FPDF->__construct() #2 {main} thrown in C:\xampp\htdocs\ts12\fpdf\fpdf.php on line 1043
Member: martenk
martenk Mar 08, 2023 at 14:32:58 (UTC)
Goto Top
das ist die staff
<?php
require('fpdf/fpdf.php');  
    
    include('classes/Core.php');  

        $id_staff=null;
        $date=null;
        //echo $_GET['id_customer']; 
        if(isset($_GET['id_staff']) && is_numeric($_GET['id_staff'])){  
           $id_staff=abs($_GET['id_staff']);  
        }
        if(is_null($id_staff) || $id_staff == ""){  
            echo "Leider konnte die Anfrage nicht bearbeitet werden.";  
            exit;
        }
        
        if(isset($_GET['date'])){  
           $date=$_GET['date'];  
           $date = date("Y-m-d", strtotime(urldecode($date)));  
        }
        
        
        
        $newLocale = setlocale(LC_TIME, 'de_DE', 'de_DE.UTF-8');  
        
        //echo $id_customer;
        Core::connectDB();
        if(isset($date)){
            $treatment_dates = Core::getTreatmentDate($date, $id_staff);
        }
        else{
            $treatment_dates = Core::getTreatmentDatesByStaff($id_staff);
        }
        $staff = Core::getStaff($id_staff);
        
  //      echo var_dump($treatment_dates);
//        echo var_dump($staff);
//        exit;
        $pdf = new FPDF();
        $pdf->AddPage();
        
        
        $id_company= "";  

        $pdf->SetTextColor(57,57,57);
        $pdf->SetFont('helvetica', '', 20);  
        $pdf->Cell(0,20, utf8_decode('Terminübersicht'));  
        $pdf->Ln(20);
        
        $pdf->SetTextColor(57,57,57);
        $pdf->SetFont('helvetica','',11);  
        $PosXSurname= $pdf->GetX();
        $PosYSurname=$pdf->GetY();
        
        $pdf->Cell( 50,6,  html_entity_decode(utf8_decode('Nachname: ')),0 ) ;  
        $pdf->SetTextColor(12,12,12);
        $pdf->Cell( 50,6,  html_entity_decode(utf8_decode($staff['surname'])),0 ) ;  
        $pdf->Ln();
        $pdf->SetTextColor(57,57,57);
        $pdf->Cell( 50,6,  html_entity_decode(utf8_decode('Vorname: ')),0 ) ;  
        $pdf->SetTextColor(12,12,12);
        $pdf->Cell( 50,6,  html_entity_decode(utf8_decode($staff['prename'])),0 ) ;  
        
        
//        $pdf->Cell( 50,6,  html_entity_decode(utf8_decode('Nachname: ')),0 ) ; 
//        $pdf->SetTextColor(12,12,12);
//        $pdf->Cell( 50,6,  html_entity_decode(utf8_decode($staff['surname'])),0 ) ; 
//        $pdf->Ln();
//        $pdf->SetTextColor(57,57,57);
//        $pdf->Cell( 50,6,  html_entity_decode(utf8_decode('Vorname: ')),0 ) ; 
//        $pdf->SetTextColor(12,12,12);
//        $pdf->Cell( 50,6,  html_entity_decode(utf8_decode($staff['prename'])),0 ) ; 
//        $pdf->Ln();
//        $pdf->SetTextColor(57,57,57);
//        $pdf->Cell( 50,6,  html_entity_decode(utf8_decode('Geburtstag: ')),0 ) ; 
//        $pdf->SetTextColor(12,12,12);
//        $pdf->Cell( 50,6,  html_entity_decode(utf8_decode($customer['birthday'])),0 ) ; 
//        $pdf->Ln();
//        $pdf->SetTextColor(57,57,57);
//        $pdf->Cell( 50,6,  html_entity_decode(utf8_decode('Straße: ')),0 ) ; 
//        $pdf->SetTextColor(12,12,12);
//        $pdf->Cell( 50,6, utf8_decode($customer['street']),0 ) ; 
//        $pdf->Ln();
//        $pdf->SetTextColor(57,57,57);
//        $pdf->Cell(50,6, 'Ort: ', 0); 
//        $pdf->SetTextColor(12,12,12);
//        $pdf->Cell(55,6, utf8_decode($customer['zipcode']). " " . utf8_decode($customer['town'])); 
//        $pdf->SetTextColor(57,57,57);

        $pdf->Ln(20);
        
        
        $pdf->Cell(10, 6, "", 'B',0);  
        $pdf->Cell(30, 6, "Wochentag", 'B');  
        $pdf->Cell(30, 6, "Datum", 'B');  
        $pdf->Cell(15, 6, "Beginn", 'B');  
        $pdf->Cell(15, 6, "Ende", 'B');  
        $pdf->Cell(30, 6, "Behandlung", 'B');  
        $pdf->Cell(60, 6, "Patient", 'B');  
        
        $pdf->Ln();
        $count = 1;
        foreach($treatment_dates as $td){
//            $d = DateTime::createFromFormat('d.m.Y H:i', $td['treatment_date']); 
            $d = strtotime($td['treatment_date']);//date('d.m.Y H:i', strtotime($td['treatment_date']));  
            $now = strtotime('now');//date("Y-m-d H:i:s");  
            if($d > $now || $date){
//            $interval = $now->diff($d);
            
            $pdf->SetTextColor(57,57,57);
            $pdf->Cell(10,6, $count++. ".)",0);  
            $pdf->SetTextColor(12,12,12);
            $td_date = split(' ', $td['treatment_date']);  
            
            $day = strftime('%A', strtotime($td['treatment_date']));  
            $pdf->Cell(30,6, $day);
            $pdf->Cell(30,6, $td_date);
            $pdf->Cell(15,6, $td_date[1]);
            
            $pdf->Cell(15,6, $td['treatment_time_end']);  
            $treatment_name = Core::getTreatment($td['id_treatment']);  
            $pdf->Cell(30, 6, $treatment_name['shortcut']);  
//            $staff = Core::getStaff($td['id_staff']); 
//            $pdf->Cell(30,6, $staff['surname']); 
//            $pdf->Cell(20,6, $d);
            $patient = Core::getPatient($td['id_patient']);  
            $pdf->Cell(60, 6, html_entity_decode(utf8_decode($patient['surname'])).', '.html_entity_decode(utf8_decode($patient['prename'])));  
            $pdf->Ln();
            }
        }
        Core::closeDB();
        $pdf->Output();
        
        exit;

        


?>
Member: martenk
martenk Mar 08, 2023 updated at 14:38:36 (UTC)
Goto Top
http://fpdf.de/Releases/articles/release-1-81.html

das fpdf ab z 1000
	switch($dest)
	{
		case 'I':  
			// Send to standard output
			$this->_checkoutput();
			if(PHP_SAPI!='cli')  
			{
				// We send to a browser
				header('Content-Type: application/pdf');  
				header('Content-Disposition: inline; filename="'.$name.'"');  
				header('Cache-Control: private, max-age=0, must-revalidate');  
				header('Pragma: public');  
			}
			echo $this->buffer;
			break;
		case 'D':  
			// Download file
			$this->_checkoutput();
			header('Content-Type: application/x-download');  
			header('Content-Disposition: attachment; filename="'.$name.'"');  
			header('Cache-Control: private, max-age=0, must-revalidate');  
			header('Pragma: public');  
			echo $this->buffer;
			break;
		case 'F':  
			// Save to local file
			$f = fopen($name,'wb');  
			if(!$f)
				$this->Error('Unable to create output file: '.$name);  
			fwrite($f,$this->buffer,strlen($this->buffer));
			fclose($f);
			break;
		case 'S':  
			// Return as a string
			return $this->buffer;
		default:
			$this->Error('Incorrect output destination: '.$dest);  
	}
	return '';  
}
Member: Pjordorf
Pjordorf Mar 08, 2023 at 14:37:58 (UTC)
Goto Top
Hallo,

Zitat von @martenk:
Fatal error: Uncaught Error: Call to undefined function get_magic_quotes_runtime() in C:\xampp\htdocs\ts12\fpdf\fpdf.php:1043 Stack trace: #0 C:\xampp\htdocs\ts12\fpdf\fpdf.php(77): FPDF->_dochecks() #1 C:\xampp\htdocs\ts12\stafftopdf.php(39): FPDF->__construct() #2 {main} thrown in C:\xampp\htdocs\ts12\fpdf\fpdf.php on line 1043
Was genau hast du denn an dieser Fehlermeldung nicht gelesen bzw. Verstanden? Alles, Nichts oder nur ein Teil? Dir ist schon Klar das wir deinen Wuellcode nicht kennen und auch nicht einsehen können, sowie alles benötigte drum herum.

Gruß,
Peter
Member: martenk
martenk Mar 08, 2023 updated at 15:02:37 (UTC)
Goto Top
den magic codes Fehler habe ich nun so gelöst - lag wohl an der alten php Version

ini_set('magic_quotes_runtime', 0);

jetzt bleibt dieser Fehler
Fatal error: Uncaught Error: Call to undefined function split() in C:\xampp\htdocs\ts12\stafftopdf.php:113 Stack trace: {main} thrown in C:\xampp\htdocs\ts12\stafftopdf.php on line 113
Mitglied: 6247018886
6247018886 Mar 08, 2023 updated at 15:18:32 (UTC)
Goto Top
split() ist deprecated und wurde in PHP 7 entfernt!! ... bitte ersetzen durch str_split oder preg_split.
http://php.adamharvey.name/manual/en/function.split.php
Member: Pjordorf
Pjordorf Mar 08, 2023 updated at 16:45:44 (UTC)
Goto Top
Hallo,

Zitat von @martenk:
lag wohl an der alten php Version
Immer sind andere Schuld


jetzt bleibt dieser Fehler
Fatal error: Uncaught Error: Call to undefined function split() in C:\xampp\htdocs\ts12\stafftopdf.php:113 Stack trace: {main} thrown in C:\xampp\htdocs\ts12\stafftopdf.php on line 113
Schon mal drüber nachgedacht das dein PHP Code verwendet der nun nicht mehr unterstützt wird, sich Aufrufe geändert haben usw.? Frag den alten Codeersteller für welche PHP Version er seinen Code erstellt hat, oder frag den Server wo du den Code herhast welche PHP Version der wohl nutzt (da läuft es wohl ohne fehler) oder schaue nach was sich in PHP alles seit Version 3/4/5 usw. geändert/getan hat. Z.B. hier https://www.php.net/manual/en/migration80.incompatible.php, sonst bist du nächstes Jahr hier noch mit dein uns unbekanntes PHP.
https://www.php.net/releases/8.0/en.php
https://www.php.net/manual/en/migration80.php
https://kinsta.com/blog/php-8-2/
https://support.deskpro.com/nl/kb/articles/php-ini-changes-aren-t-workin ...
https://www.phparch.com/2022/10/whats-changed-in-php-8-2/
https://developers.slashdot.org/story/20/11/28/1030246/php-80-brings-maj ...
https://durak.org/sean/pubs/software/php-7.4.3/migration56.openssl.html

Gruß,
Peter
Member: GrueneSosseMitSpeck
GrueneSosseMitSpeck Mar 08, 2023 at 19:03:04 (UTC)
Goto Top
Die mysql_xxxxx Befehle aus dem PHP5 gehen im aktuellen PHP Level nicht mehr, bzw sind bei einigen Providern abgeklemmt, bei anderen nicht je nacdem wie die ihre Scriptengines konfiguriert haben.

Für eine einfache Migration hat der Schöpfer vom PHP das mysqli_ erfunden. Hab selber gerade ein Dutzend PHP Skripte durchgearbeitet. Dann muß man nicht die objektorientierte Variante nehmen, was unter Umständen einen Riesen Umstellungsaufwand bedeutet. Bei unserer PHP basierten Seite waren das 8 Dateien und ca. 100 Codestellen, die man allesamt mit einem automatisierten Ersetzen konvertieren konnte.

PHP5 wird bei praktisch allen Providern so langsam ad acta gelegt. Und bei einigen MySQLI Aufrufen sind Datenbank und Host vertauscht. Ist vom Prinzip her sehr einfach zu machen, meines Erachtens nur Fleißarbeit.

https://www.php.net/manual/de/book.mysqli.php
Member: martenk
martenk Mar 09, 2023 at 16:48:06 (UTC)
Goto Top
könnt ihr bitte einmal schauen bei diesem code

$td_date = split(' ', $td['treatment_date']);

wie würdet ihr das machen
Mitglied: 6247018886
6247018886 Mar 09, 2023 updated at 17:04:43 (UTC)
Goto Top
Zitat von @martenk:

könnt ihr bitte einmal schauen bei diesem code

$td_date = split(' ', $td['treatment_date']);

Au möhr, heute schon wieder Freitag?
RTFM oben schon verlinkt, liest hier keiner mehr die Links die man einem an die Hand gibt ???
http://php.adamharvey.name/manual/en/function.preg-split.php
$td_date = preg_split('/ /', $td['treatment_date']);  
🚑💉
Member: martenk
martenk Mar 09, 2023 at 17:05:31 (UTC)
Goto Top
$td_date = split(' ', $td['treatment_date']);

ich habe es nun so gemacht
$td_date = explode(' ', $td['treatment_date']);

bekomme nun allerdings ein englisches Format angezeigt