PHP Script ändern damit mysqli / PHP 7.2 funktioniert

Status
Dieses Thema wurde gelöst! Zur Lösung gehen…

cliodriver

Mitglied
Hallo

das ist seit langem wieder mal eine Frage / Bitte um Hilfe zu einem PHP-Script.

Ich habe vor einem Jahr eine Homepage für eine Bekannte (Ferienwohnungen) erstellt und ein fertiges Script für einen Belegungskalender eingebaut und dabei nicht bedacht / gewusst das der Server demnächst auf PHP 7.2 umstellt. Ich konnte schon testen und den Rest der Homepage anpassen, doch leider bin ich mit meinem Latein am ende und der Kalender funktioniert natürlich nicht.

Das Problem liegt natürlich an der Datenbankverbindung .... mysqli unter PHP 7.2, welche ich nicht gefixt bekomme

Meine Frage dazu:

Kann sich jemand den Code mal ansehen und sagen er sich anpassen lässt bzw. hätte gleich Tipps für mich? Ich würde gerne verstehen wo der Hund begraben liegt und es selber ändern, doch ohne Hilfe komm ich grad nicht weiter.

Weiters umfasst das ganze ca. 19 Seiten und wenn ich die alle hier poste, wird's wahrscheinlich unübersichtlich. Ich würde das Paket per email versenden wenn ich Hilfe bekomme.

Danke
 
19 Seiten ist schon viel.Meistens brauchst du ja nur die Zeilen ändern die mit Datenbank zu tun haben.. Was hast du den bis jetzt benutzt ? mysql ohne i ? Fals ja brauch man ja nur die Zeilen ändern wo das i Fehlt und meistens noch einen 2ten Parameter mitgeben
 
Wenns nur so einfach wäre, das hab ich schon mehrfach versucht mit dem Ergebnis dass garnichts mehr funktioniert. Irgendwo überseh ich was

Ja, es wird mysql verwendet und ich hab das i schon drangehängt, sogar versucht das ganze script umzubasteln...
 
einfach nur ein "i" reinschreiben reicht meistens nicht. bei vielen sachen wird noch ein 2 Parameter benötigt. Das umzuschreiben von mysql zu mysqli habe ich schon paar mal gemacht aber keins was über19 Seiten geht.
Wo kommst du denn nicht weiter ? Vieleicht versuchst du es nochmal und schreibst dann hier den Fehlercode mit depassende Zeile rein dann kam man dir ja sagen was bei den einzelnden sachen geändert werden muß.

Was ist das eigentlich für ein Script das es so groß ist?
 
dass das "i" nicht reicht, ist klar und konnte ich auch beim Rest der HP den ich selber gemacht habe anwenden.

die 19 Seiten bzw. *.php Dateien sind das gesamte Konstrukt dieses Belegungskalenders inkl. Adminbereich und Erstinstallationstool (was eigentlich nicht benötigt wird)

nicht jede *.php enthält auch datenbankrelevante scripte

aber anbei ein paar code-schnipsel um zu sehen wies ungefähr läuft

PHP:
<?php

// Kalender Version
define ('VERSION', '2.1.4');

// PHP version
$php_version_required = '5.0.0';
$php_version_current = phpversion ();
if (version_compare ($php_version_current, $php_version_required, '<')) {
    die ('ERROR: PHP version ' . $php_version_required . ' or later required! You have installed ' . $php_version_current . '.');
}

// Session
session_start ();

// Includes
require_once BASEDIR . 'includes/db.class.php';
require_once BASEDIR . 'includes/cal.class.php';
require_once BASEDIR . 'includes/functions.inc.php';
require_once BASEDIR . 'includes/config.inc.php';

// Variables
$r = ''; // Output buffer
$pageTitle = '';
$lang = '';
$langFromUser = false;

// PHP runtime settings
if (DEBUG) {
    ini_set ('display_errors', 1);
    error_reporting (E_ALL);
}
else {
    ini_set ('display_errors', 0);
}

// Language part 1: before db connection
// If language is stored in GET or SESSION, use this
if (isset ($_GET['lang']))
    $lang = $_GET['lang'];
elseif (isset ($_SESSION['lang']))
    $lang = $_SESSION['lang'];
// Validate or use de as default
$lang = preg_replace ('/[^a-z_]*/', '', $lang);
$langFile = BASEDIR . 'lang/' . $lang . '.inc.php';
if (file_exists ($langFile))
    $langFromUser = true;
else {
    $lang = 'de';
    $langFile = BASEDIR . 'lang/' . $lang . '.inc.php';
}
if (file_exists ($langFile)) {
    require_once $langFile; // Include
    $_SESSION['lang'] = $lang; // Store in SESSION
}
else
    trigger_error ('Language file "' . $langFile . '" not found.', E_USER_ERROR);

// Connect to db and check for errors
$db = new DatabaseConnection ($mysql);
if ($db -> error) {
    $pageTitle = __('error');
    switch ($db -> error) {
        case 1:
            $r .= echoError (__('errorDBAuth'));
            break;
        case 2:
            $r .= echoError (__('errorDBSetupNoDB'));
            break;
        case 3:
            $r .= echoError (__('errorDBSetupNoTables'));
            break;
        case 4:
        case 5:
            $r .= echoError (__('errorDBSetupVersionsDiffer'));
    }
}

// Jobs to do if successfull connected
else {
    // Read preferences
    $prefs = readPrefs ($db);
    // Language part 2: use default language from db, if hard-coded default chosen in part 1
    if (!$langFromUser) {
        $result = $db -> select ("SELECT `abbr` FROM `languages` WHERE `id`=" . $prefs['languageDefault']);
        $langFile = BASEDIR . 'lang/' . $result[0]['abbr'] . '.inc.php';
        if (file_exists ($langFile)) {
            $lang = $result[0]['abbr'];
            require_once $langFile; // Include
            $_SESSION['lang'] = $lang; // Store in SESSION
        }
    }
    // Get language id
    $result = $db -> select ("SELECT `id` FROM `languages` WHERE `abbr`='" . $lang . "'");
    define ('LANG_ID', $result[0]['id']);
    // Get active languages
    $GLOBALS['langsActive'] = readLanguages ();
}

// Define Language
define ('LANG_ABBR', $lang);

und ...

PHP:
<?php
// Class for database connection
class DatabaseConnection {

    public    $error = false;
    public    $version = false;
    private    $link = false;
    private $config = array ();

    /**
     * Wrapper for connect ()
     * @param    $db        Array with vars from config
     * @return            result from connect ()
     */
    public function databaseConnection ($mysql) {
        $this -> config = $mysql;
        return $this -> connect ();
    }

    /**
     * Tries to connect to db and checks installation.
     * @return            true if no error, else false
     */
    public function connect () {
        // Reset $error
        $this -> error = false;
        // Try to connect to db
        $this -> link = @mysql_connect ($this -> config['host'] . ':' . $this -> config['port'], $this -> config['user'], $this -> config['pass']);
        if (!$this -> link)
            $this -> error = 1; // Connection failed.
        else {
            if (function_exists ('mysql_set_charset'))
                mysql_set_charset ('utf8', $this -> link);
            // Try to select db
            if (mysql_select_db ($this -> config['name'], $this -> link)) {
                // Look for prefs table
                $version = $this -> select ("SELECT `value` FROM `prefs` WHERE `name`='dbVersion'");
                if (!$version) {
                    // Check for v1.x installation with this year's table
                    $tables = $this -> select ("SHOW TABLES");
                    if ($tables) {
                        foreach ($tables as $table) {
                            if (in_array ($this -> config['pfix'] . date ('Y'), $table)) {
                                $this -> error = 5; // v1.x installation found
                                break;
                            }
                        }
                    }
                    if (!$this -> error)
                        $this -> error = 3; // Table does not exist
                }
                // Check for db version
                else {
                    $this -> version = $version[0]['value'];
                    if ($this -> version != VERSION)
                        $this -> error = 4; // DB-Version and Files-Version differ
                }
            }
            else
                $this -> error = 2; // Database does not exist
        }
        if ($this -> error)
            return false;
        else
            return true;
    }
   
usw....
 
Zuletzt bearbeitet:
...
PHP:
<?php
// Class for database connection
class DatabaseConnection {

    public    $error = false;
    public    $version = false;
    private    $link = false;
    private $config = array ();

    /**
     * Wrapper for connect ()
     * @param    $db        Array with vars from config
     * @return            result from connect ()
     */
    public function databaseConnection ($mysql) {
        $this -> config = $mysql;
        return $this -> connect ();
    }

    /**
     * Tries to connect to db and checks installation.
     * @return            true if no error, else false
     */
    public function connect () {
        // Reset $error
        $this -> error = false;
        // Try to connect to db
        $this -> link = @mysqli_connect ($this -> config['host'] . ':' . $this -> config['port'], $this -> config['user'], $this -> config['pass'],$this -> config['name']);
        if (!$this -> link)
            $this -> error = 1; // Connection failed.
        else {
            if (function_exists ('mysqli_set_charset'))
                mysqli_set_charset ( $this -> link,'utf8');
                // Look for prefs table
                $version = $this -> select ("SELECT `value` FROM `prefs` WHERE `name`='dbVersion'");
                if (!$version) {
                    // Check for v1.x installation with this year's table
                    $tables = $this -> select ("SHOW TABLES");
                    if ($tables) {
                        foreach ($tables as $table) {
                            if (in_array ($this -> config['pfix'] . date ('Y'), $table)) {
                                $this -> error = 5; // v1.x installation found
                                break;
                            }
           
                    }
                    if (!$this -> error)
                        $this -> error = 3; // Table does not exist
                }
                // Check for db version
                else {
                    $this -> version = $version[0]['value'];
                    if ($this -> version != VERSION)
                        $this -> error = 4; // DB-Version and Files-Version differ
                }
            }
            else
                $this -> error = 2; // Database does not exist
        }
        if ($this -> error)
            return false;
        else
            return true;
    }

Habe mal den Code den du gepostet hast angepasst für MySqli.

Der größte unterschied zwischen Mysql und mysqli ist eigentlich das die Referenz zum Connect als erster Parameter übergeben wird.

Wenn es noch Probleme gibt einfach Posten.
 
So habe mir das mal angeschaut.

Es ist nur die db.class.php anzupassen.

PHP:
<?php
// Class for database connection
class DatabaseConnection {

    public    $error = false;
    public    $version = false;
    private    $link = false;
    private $config = array ();

    /**
     * Wrapper for connect ()
     * @param    $db        Array with vars from config
     * @return            result from connect ()
     */
    public function databaseConnection ($mysql) {
        $this->config = $mysql;
        return $this->connect ();
    }

    /**
     * Tries to connect to db and checks installation.
     * @return            true if no error, else false
     */
    public function connect () {
        // Reset $error
        $this->error = false;
        // Try to connect to db
        $this->link = @mysqli_connect($this->config['host'] . ':' . $this->config['port'], $this->config['user'], $this->config['pass']);
        if (!$this->link)
            $this->error = 1; // Connection failed.
        else {
            if (function_exists ('mysql_set_charset'))
                mysqli_set_charset($this->link,'utf8');
            // Try to select db
            if (mysqli_select_db($this->link,$this->config['name'])) {
                // Look for prefs table
                $version = $this->select ("SELECT `value` FROM `prefs` WHERE `name`='dbVersion'");
                if (!$version) {
                    // Check for v1.x installation with this year's table
                    $tables = $this->select ("SHOW TABLES");
                    if ($tables) {
                        foreach ($tables as $table) {
                            if (in_array ($this->config['pfix'] . date ('Y'), $table)) {
                                $this->error = 5; // v1.x installation found
                                break;
                            }
                        }
                    }
                    if (!$this->error)
                        $this->error = 3; // Table does not exist
                }
                // Check for db version
                else {
                    $this->version = $version[0]['value'];
                    if ($this->version != VERSION)
                        $this->error = 4; // DB-Version and Files-Version differ
                }
            }
            else
                $this->error = 2; // Database does not exist
        }
        if ($this->error)
            return false;
        else
            return true;
    }

    /**
     * Validates an id
     * @param    $table    table to query
     * @param    $id        id to check
     * @return            true if exists, else false
     */
    function idExists ($table, $id) {
        if (is_numeric ($id)) {
            $query = "SELECT `id`
                FROM `" . $table . "`
                WHERE `id`='" . $id . "'
                LIMIT 1";
            $query = $this->queryAddTablePrefixes ($query);
            $result = mysqli_query($this->link, $query);
            if (mysqli_num_rows($result)) {
                return true;
            }
            else {
                return false;
            }
        }
        else {
            return false;
        }
    }

    /**
     * Submits a query to db.
     * @param    $query    String
     * @param    $return    String, what should be returned: affected_rows | insert_id
     * @return            int affected rows, error: false
     */
    public function query ($query, $return = 'affected_rows') {
        $query = $this->queryAddTablePrefixes ($query);
        $result = mysqli_query($this->link,$query);
        if (!$result) {
            if (DEBUG)
                trigger_error (mysqli_error($this->link));
            return false;
        }
        else {
            switch ($return) {
                case 'insert_id':
                    return mysqli_insert_id($this->link);
                    break;
                default:
                    return mysqli_affected_rows($this->link);
            }
        }
    }

    /**
     * Submits a SELECT-query to db and returns rows as multidimensional array.
     * @param    $query    String
     * @return            Array, error: false
     */
    public function select ($query) {
        $query = $this -> queryAddTablePrefixes ($query);
        $result = mysqli_query($this -> link,$query);
        if (!$result) {
            if (DEBUG)
                trigger_error (mysqli_error($this->link));
            return false;
        }
        else {
            $num = mysqli_num_rows($result);
            if ($num) {
                $array = array ();
                while ($row = mysqli_fetch_assoc($result))
                    $array[] = $row;
                return $array;
            }
            else
                return false;
        }
    }

    /**
     * Creates the given db and selects it if successfull.
     * @return        true if successful, else MySQL-error
     */
    public function createDB () {
        $query = "CREATE DATABASE `" . $this->config['name'] . "`";
        $result = mysqli_query($this->link,$query);
        if (!$result)
            return mysqli_error($this->link);
        else {
            mysqli_select_db($this->link,$this->config['name']);
            return true;
        }
    }

    /**
     * Updates database to current GuestCal version.
     * @return        true if successful, else error message
     */
    public function update ($reconnect = false) {
        $errors = array ();
        if ($this->error == 4) { // update if file and db versions differ
            $result = $this->importDump ('sql/update_' . $this->version . '.mysql');
            if ($result === true) {
                $this->close();
                $this->connect();
                return $this->update();
            }
            else {
                $errors[] = 'Database could not be updated to version ' . VERSION;
                $errors = array_merge ($errors, $result);
                return $errors;
            }
        }
        else
            return true;
    }

    /**
     * Imports SQL dump from a file
     * @param    $filename    Path to file
     * @return                true if successful, else array with errors
     */
    public function importDump ($filename) {
        if (!file_exists ($filename))
            return array ('File "' . $filename . '" does not exist.');
        // Read file
        $content = file_get_contents ($filename);
        // Split into statements
        $lines = explode (";\n", $content);
        // Execute statements
        $errors = array ();
        foreach ($lines as $line) {
            if (!empty ($line)) {
                $query = $this -> queryAddTablePrefixes ($line);
                $result = mysqli_query($this->link,$query);
                if (!$result) {
                    $errors[] = 'MySQL-Error: "' . mysqli_error($this->link) . '"';
                }
            }
        }
        if (count ($errors))
            return $errors;
        else
            return true;
    }

    /**
     * Imports tables from 1.x installation.
     * @param    $prefix        table prefix of version to be imported
     * @param    $objectId    id of the object, in which the data will be imported
     * @return                number of tables imported
     */
    public function importFromV1 ($prefix = false, $objectId = 1) {
        $countImported = 0;
        if (!$prefix)
            $prefix = $this->config['pfix'];
        $newClasses = array (
            '1' => 2,
            '2' => 3
        );
        $tables = $this->select ("SHOW TABLES");
        if ($tables) {
            foreach ($tables as $table) {
                $year = array_pop ($table);
                $year = str_replace ($prefix, '', $year);
                if (is_numeric ($year) && $year > 1970 && $year < 2038) {
                    $array = $this->select ("SELECT `tag`, `belegt`, `infotext` FROM `" . $prefix . $year . "`");
                    $belegt = 0;
                    $infotext = '';
                    $from = false;
                    $to = false;
                    foreach ($array as $row) {
                        $month = substr($row['tag'], 0, 2);
                        $day = substr($row['tag'], 2, 2);
                        if (@checkdate ($month, $day, $year)) {
                            $now = $year . '-' . $month . '-' . $day;
                            if ($row['belegt'] != $belegt or $row['infotext'] != $infotext) {
                                if ($belegt != 0 || $infotext != '') {
                                    $id = $this->query ("INSERT INTO `entries` SET `object_id`=" . $objectId . ", `class_id`=" . $newClasses[$belegt] . ", `kind`='static'", 'insert_id');
                                    $this->query ("INSERT INTO `entries_static` SET `entry_id`=" . $id . ", `from`='" . $from . "', `to`='" . $to . "'");
                                    $this->query ("INSERT INTO `entries_per_language` SET `entry_id`=" . $id . ", `language_id`=1, `desc`='" . $infotext . "'");
                                }
                                $belegt = $row['belegt'];
                                $infotext = $row['infotext'];
                                $from = $now;
                            }
                        }
                        $to = $now;
                    }
                    $countImported ++;
                }
            }
        }
        return $countImported;
    }

    /**
     * Closes connection.
     */
    public function close () {
        if ($this->link)
            return mysqli_close($this->link);
    }

    /**
     * Adds prefix to table names in query.
     * @param    $query    String
     * @return            String
     */
    private function queryAddTablePrefixes ($query) {
        $naked_tables = array (
            '`classes`',
            '`classes_per_language`',
            '`entries`',
            '`entries_per_language`',
            '`entries_static`',
            '`entry_returning`',
            '`languages`',
            '`objects`',
            '`objects_per_language`',
            '`prefs`',
            '`users`'
        );
        $prefixed_tables = array ();
        foreach ($naked_tables as $table)
            $prefixed_tables[] = '`' . $this->config['pfix'] . substr ($table, 1);
        $query = str_replace ($naked_tables, $prefixed_tables, $query);
        return $query;
    }

    function mysql_real_escape_mixed ($var) {
        if (is_array ($var)) {
            foreach ($var as $name => $value) {
                if (is_array ($value)) {
                    $var[$name] = $this->mysql_real_escape_mixed ($value);
                }
                else {
                    $var[$name] = mysqli_real_escape_string($this->link,$value);
                }
            }
        }
        else {
            $var = mysqli_real_escape_string($this->link,$var);
        }
        return $var;
    }

    private function stripslashes_a ($array) {
        if (count ($array) > 0) {
            foreach ($array as $name => $value) {
                if (is_array ($value)) {
                    $array[$name] = stripslashes_a ($value);
                }
                else {
                    $array[$name] = stripslashes ($value);
                }
            }
        }
        return $array;
    }
}

Habe den Code nicht auf Funktion geprüft sondern rein auf Syntax und da gibt meine IDE keinen Fehler aus.
 
Status
Dieses Thema wurde gelöst! Zur Lösung gehen…
Zurück