Announcement

Collapse
No announcement yet.

Klassen in PHP

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Klassen in PHP

    Hallo,

    ich bin gerade dabei mich mit der OOP in PHP einzulesen. Als Lernprojekt habe ich ein Login Script gewählt. Dieses Projekt hat zwei Klassen: DB und Login.

    <PRE>
    class DB {
    var $conID;
    var $result;

    /* Konstruktor */
    function DB (){
    $this->conID = conn;
    $this->result = res;
    }
    /* Konstruktor - Ende */

    function connect_db($host, $user, $pass) {
    if(!$this->conn = mysql_connect($host, $user, $pass)) {
    die (mysql_error());
    }
    return $this->conn;
    }

    function select_db($db) {
    if(!mysql_select_db($db, $this->conn)) {
    die (mysql_error());
    }

    function execute_sql($sql) {
    if(!$this->res = mysql_query($sql, $this->conn)) {
    die("Die SQL-Anweisung konnte nicht ausgeführt werden");
    }
    return $this->res;
    }
    }

    class Login extends DB {
    function test() {
    parent:B();
    $res_id = $this->execute_sql("SELECT id FROM user");
    while($row = mysql_fetch_array($res_id)) {
    echo "ID: ".$row[0];
    }
    }
    }
    </PRE>

    Wenn ich jetzt das Script ausfürhre

    <PRE>
    include('db_data.php');
    include('classes2.php');

    $db = new DB;
    $db->connect_db($db_host, $db_user, $db_pass);
    $db->select_db($database);
    $result = $db->execute_sql("SELECT * FROM user");

    $test = new Login;
    $test->test();
    </PRE>

    erhalte ich folgende Meldung:

    "Warning: Supplied argument is not a valid MySQL-Link resource on line 29"

    Line 29 stammt von der Funktion execute_sql aus der Klasse DB:

    <PRE>
    if(!$this->res = mysql_query($sql, $this->conn))
    </PRE>

    Was mache ich falsch?

    Gruß Sven

  • #2
    <p>Hallo,<br />
    <br />
    ich würde die fragliche Stelle einmal anders formulieren:<br />
    <pre>
    function execute_sql($sql)
    {
    $this->res=mysql_query($sql, $this->conn);
    if (!this->res) die("Die SQL-Anweisung konnte nicht ausgeführt werden");
    else return $this->res;
    }
    </pre>
    Was mich stutzig mach ist dein Konstruktor.
    <pre>
    function DB()
    {
    $this->conID = conn;
    $this->result = res;
    }
    woher kommen <b>conn</b> und <b>res</b>?
    </pre>
    <br />
    Gruß Thoma

    Comment

    Working...
    X