ThWboard Support-Forum (Archiv)

Ort: / Boardübersicht / Test-Board / php


Seite 1 von 1

philippgerard schrieb am 25.01.2003 um 14:27 Uhr

<?

class mysql {

    var $username;
    var $password;
    var $host;
    var $database;
    var $link;
    var $result;
    var $last_query;
    var $current_table;
        var $numrows;
        var $numfields;
        var $fields;
        var $init_done;

        function mysql () {

                $this->init_done = 0;
                $this->numrows = 0;
                $this->numfields = 0;
                $this->fields[0] = "";

        }

        function init ($host = "_not-set_", $db = "_not-set_", $user = "_not-set_", $pass = "_not-set_") {
               
                if ( $host == "_not-set_" ||
                     $pass == "_not-set_" ||
                     $host == "_not-set_" ||
                     $db == "_not-set_"
                   ) 
                   
                {
                       
                        echo "<b>init variables not set. usage: obj->init
                                (host, db, user, pass)</b><br />";
                
                }

                else {

                        $this->host = $host;
                $this->username = $user;
                $this->password = $pass;
                $this->database = $db;
                        $this->init_done = 1;
                        $this->open();
               
                }

        }

        function setHost ($hostname = "_not-set_") {
                $this->host = $hostname;

        }

        function setDatabase ($db = "_not-set_") {
                $this->database = $db ;
        }

        function setUser ($user = "_not-set_") {
                $this->username = $user;
        }

        function setPassword ($newpassword = "_not-set_") {
        $this->password = $newpassword;

        }

    function open () {
                if ($this->init_done == 1) {
                        $this->link = mysql_connect($this->host, $this->username,
                                $this->password) or
                                die ("Could not connect.");
                mysql_select_db ($this->database) or
                                die (" DB: function open: Could not select database.");
                }
                else {
                        echo "<center><b>Class DB not initialized.
                                Use 'init' function to do so.</b><br /></center>";
                }
    }
    function close () {
        mysql_close($this->link);
    }

    function setDB ($newDatabase) {
        $this->database = $newDatabase;
        mysql_select_db ($this->database)
            or die ("DB: function setDB: Could not re-select database to \'$newDatabase\'");
    }

    function do_query ($query) {
                
                $query = preg_replace ("/;.*/", "", $query);
                $patterns = array("/DROP.*/i","/^DELETE/i");
        
                if ( !(preg_match ("/DROP.*/i", $query)) and !(preg_match ("/^DELETE/i", $query)) ) {
                        
                        $this->last_query = $query;
            
                        // echo "$query";                // Debug
            
                        $resultid = mysql_query ($query) or die ("<b>Query failed (function do_query: $query)</b><br />");

            // echo "<b>$resultid</b>";      // Debug

            if ( preg_match("/^SELECT/i", $query) ) {
                            $rows = mysql_num_rows($resultid);
                            $this->numfields = mysql_num_fields ($resultid);
                        $this->getFields($resultid);

            }
            else {
                            $rows = mysql_affected_rows($this->link);
                            $this->numfields = -1;
            }

        }
        else {
            echo "<b>Verboten.</b><br />";

                        $resultid = "ERR_do_query()";
        }

                $this->result = $resultid;
                $this->numrows = $rows;
                $this->numfields = $fields;
        }

    function do_query_secure ($query,$user) {

                $query = preg_replace ("/;.*/", "", $query);
        $this->last_query = $query;
                $resultid = mysql_query ($query) or die ("<b>Query failed (function do_query_secure: $query)</b>");

                if ( preg_match("/^SELECT/i", $query) ) {

                    $rows = mysql_num_rows($resultid);
                        $this->numfields = mysql_num_fields ($resultid);
                        $this->getFields($resultid);

                }

                else {

                    $rows = mysql_affected_rows($this->link);
                        $this->numfields = -1;

               }


                $this->result = $resultid;
                $rows = mysql_num_rows($resultid);
                $this->numrows = $rows;

    }

    function getResult () {
        return $this->result;
    }

    function getLastQuery () {
        return $this->last_query;
    }

    function getRow () {
        return (mysql_fetch_row($this->result));
    }

    function getRowExtended () {
        return (mysql_fetch_array($this->result));
    }

    function displayResultTable () {
                if ($this->result != false) {

                        mysql_data_seek($this->result, 0);

                        //echo "Rows: " . $this->numrows . "<br />" ;     //Debug
                        //echo "Fields: " . $this->numfields . "<br />" ; //Debug

            echo "<table border=1>\n";
                        echo "<tr>";
                        foreach ($this->fields as $field) {
                                echo "<th>$field</th>";
                        }
                        echo "</tr>\n";
            while ($line = $this->getRow()) {
                                echo "<tr>";
                foreach ($line as $value) {
                                        echo "<td>$value</td>";
                }
                echo "</tr>\n";
            }
            echo "</table>\n";
        }
        else {
            echo "<b>No Records returned</b><br />\n";
        }
    }
        
        function getFields($result) {
                @mysql_data_seek($result, 0);

                // echo "<b>Entered GetFields(numFields: " // Debug
                // . $this->numfields . ")</b><br />";     // Debug
                $i = 0;
                while ($i < $this->numfields) {

                        //echo "<b>Entered While loop</b><br />";  //Debug

                        $currField = mysql_fetch_field($result, $i);
                        $this->fields[$i] = $currField->name;
                        $i++;

                        //echo "<b>Finished one pass (". $this->fields[$i] . " = " // Debug
                        // . $currField->name . ")</b><br />";                     // Debug

                }
                @mysql_data_seek($result, 0);
        }

    function clearResult () {
        mysql_free_result ($this->link);
        $this->result = false;
    }

    function getNumRows () {
        return ($this->numrows);
    }

        function getNumFields () {
        return ($this->numfields);
    }

}

?>

philippgerard schrieb am 25.01.2003 um 14:28 Uhr

Warum denn kein Syntaxhighlightning? grmbl...

Bluetooth schrieb am 25.01.2003 um 14:31 Uhr

weil das wegen der php-version hier nicht funktioniert

Adrian schrieb am 27.01.2003 um 21:00 Uhr

Bluetooth postete
weil das wegen der php-version hier nicht funktioniert

was läuft hier denn für eine? *g*

Seite 1 von 1