Farmer: Insert SQL Befehl über PHP

Beitrag lesen

OK,

ich habe eine Datei CTable.php in der die Befehle ausgeführt werden. An diese Datei übergebe ich dann die Parameter.

<?php
if (!defined("__CTable__")) {
    define("__CTable__", true);

class CTable {
    var $Enabled;
    var $Username;
    var $Password;
    var $Database;
    var $Table;
    var $RecordCount;
    var $RecordNo;
    var $Bof;
    var $Eof;
    var $Filtered;
    var $Filter;    // WHERE
    var $Sort;      // ORDER BY
    var $Conn;      // Connection ID
    var $Rs;        // Result Set

var $FirstPage;
    var $PrevPage;
    var $NextPage;
    var $LastPage;
    var $PageSize;
    var $PageCount;
    var $Eop;

function SetPage($page = 1)
    {
        if (!$this->Enabled) {
            print("Error: Please open table first.");
            exit();
        }
        if ($this->RecordCount == 0) {
            $this->Eop = true;
            return;
        }
        $this->ShowCount = 1;
        $this->Eop = false;

$this->PageCount = (int)(($this->RecordCount - 1) / $this->PageSize) + 1;
        if ($page < 1) {
            $page = 1;
        }
        elseif ($page > $this->PageCount) {
            $page = $this->PageCount;
        }

$this->RecordNo = ($page - 1) * $this->PageSize + 1;
        odbc_fetch_row($this->Rs, $this->RecordNo);

$this->FirstPage = 1;
        $this->LastPage = $this->PageCount;
        $this->PrevPage = $page - 1;
        if ($this->PrevPage < 1) {
            $this->PrevPage = 1;
        }
        $this->NextPage = $page + 1;
        if ($this->NextPage > $this->LastPage) {
            $this->NextPage = $this->LastPage;
        }
    }

function CTable($database = "", $table = "", $username = "", $password = "")
    {
        $this->Username = $username;
        $this->Password = $password;
        $this->Database = $database;
        $this->Table = $table;
        $this->RecordCount = 0;
        $this->RecordNo = 0;
        $this->Bof = false;
        $this->Eof = false;
        $this->Enabled = false;
        $this->Filtered = false;
        $this->Filter = "";
        $this->Conn = false;
        $this->Rs = false;
        $this->PageSize = 10;
    }

function SetFilter($sql = false)
    {
        if ($sql == false) {
            $this->Filter = "";
            $this->Filtered = false;
        }
        else {
            $this->Filter = "where $sql";
            $this->Filtered = true;
        }
    }

function SetSort($sql = false)
    {
        if ($sql == false) {
            $this->Sort = "";
        }
        else {
            $this->Sort = "order by $sql";
        }
    }

function Open($sql = "sql")
    {
        $this->Conn = odbc_connect($this->Database, $this->Username, $this->Password);
        if ($this->Conn == false) {
            print("ERROR: Connect to database $this->Database failed.<br>");
            exit();
        }

if ($sql == "sql") {
            if ($this->Filtered) {
                $this->Rs = odbc_exec($this->Conn, "select count(*) as counter from $this->Table $this->Filter");
            if ($this->Rs == false) {
                    print("ERROR: Open table $this->Table failed.<br>");
                    exit();
                }
                $this->RecordCount = odbc_result($this->Rs, "counter");
                odbc_free_result($this->Rs);

$this->Rs = odbc_exec($this->Conn, "select * from $this->Table $this->Filter $this->Sort");
                if ($this->Rs == false) {
                    print("ERROR: Open table $this->Table failed.<br>");
                    exit();
                }
            }
            else {
                $this->Rs = odbc_exec($this->Conn, "select count(*) as counter from $this->Table");
                if ($this->Rs == false) {
                print("ERROR: Open table $this->Table failed.<br>");
                    exit();
                }
                $this->RecordCount = odbc_result($this->Rs, "counter");
                odbc_free_result($this->Rs);

$this->Rs = odbc_exec($this->Conn, "select * from $this->Table $this->Sort");

if ($this->Rs == false) {
                    print("ERROR: Open table $this->Table failed.<br>");
                    exit();
                }
            }
        }
        else {
            $this->Rs = odbc_exec($this->Conn, $sql);

if ($this->Rs == false) {
                print("ERROR: Open table $this->Table failed.<br>");
                exit();
            }
            $rows = odbc_num_rows($this->Rs);
            if ($rows == -1) { // some ODBC drivers return -1 so we count it(very slow)
                $rows = 0;
                while (odbc_fetch_row($this->Rs)) {
                    $rows++;
                }
            }
            $this->RecordCount = $rows;
        }
        odbc_fetch_row($this->Rs, 1);
        $this->RecordNo = 1;
        $this->Enabled = true;
        $this->SetPage(1);
        if ($this->RecordCount == 0) {
            $this->Bof = true;
            $this->Eof = true;
            $this->Eop = true;
        }
    }

function Close()
    {
        if (!$this->Enabled) {
            print("Error: No table opened.");
            exit();
        }
        odbc_free_result($this->Rs);
        // odbc_close($this->Conn);
        $this->Enabled = false;
    }

function First()
    {
        if (!$this->Enabled) {
            print("Error: Please open table first.");
            exit();
        }
        $this->RecordNo = 1;
        odbc_fetch_row($this->Rs, 1);
    }

function Prev()
    {
        if (!$this->Enabled) {
            print("Error: Please open table first.");
            exit();
        }
        $this->RecordNo = $this->RecordNo - 1;
        if ($this->RecordNo < 1) {
            $this->RecordNo = 1;
            $this->Bof = true;
        }
        odbc_fetch_row($this->Rs, $this->RecordNo);
    }

function Next()
    {
        if (!$this->Enabled) {
            print("Error: Please open table first.");
            exit();
        }
        $this->RecordNo = $this->RecordNo + 1;
        if ($this->RecordNo > $this->RecordCount) {
            $this->RecordNo = $this->RecordCount;
            $this->Eof = true;
        }
        odbc_fetch_row($this->Rs, $this->RecordNo);
        $this->ShowCount = $this->ShowCount + 1;
        if (($this->ShowCount > $this->PageSize) || $this->Eof) {
            $this->Eop = true;
        }
    }

function Last()
    {
        if (!$this->Enabled) {
            print("Error: Please open table first.");
            exit();
        }
        $this->RecordNo = $this->RecordCount;
        odbc_fetch_row($this->Rs, $this->RecordNo);
    }

function FieldByName($field)
    {
        if (!$this->Enabled) {
            print("Error: Please open table first.");
            exit();
        }
        return odbc_result($this->Rs, $field);
    }

// this function will be removed someday...
    function Exec($sql)
    {
        if (!$this->Enabled) {
            print("Error: Please open table first.");
            exit();
        }
        return odbc_exec($this->Conn, $sql);
    }

function Goto($num)
    {
        if (!$this->Enabled) {
            print("Error: Please open table first.");
            exit();
        }
        $this->RecordNo = $num;
        if ($num > $this->RecordCount) {
            $this->RecordNo = $this->RecordCount;
        }
        elseif ($num < 1) {
            $this->RecordNo = 1;
        }
        odbc_fetch_row($this->Rs, $this->RecordNo);
    }
};
}
?>