Hi,

I cannot find the solution for this example. It is to do with Static declerations. Help please.

Error : "Fatal error: Non-static method ExecuteSql::executeSql() cannot be called statically, assuming $this from incompatible context "

Thanks

<?php
class DBconnection {
	private $host="localhost";
	private $user="root";
	private $username=null;
	private $password=null;
	private $database="classTest";
		
	protected $conn;
	private $db;
	
	public function __construct() {
		$this->conn=mysql_connect($this->host, $this->user, $this->username, $this->password);
		$this->db=mysql_select_db($this->database);
	}
}

class ExecuteSql extends DBconnection {
	public function executeSql($sql) {
		$runSql=mysql_query($sql, DBconnection::conn) or die ("ERROR: Database connection error");
		
		if (@mysql_num_rows($runSql) == 1) {
			return "VALID";
		} else {
			return "INVALID";
		}
	}
}

class Login extends DBconnection {
	public function __construct($username, $password) {
		DBconnection::__construct();
		echo ExecuteSql::executeSql("SELECT id FROM login WHERE username='$username' AND password='$password'");
	}
}
?>
<?php
require_once("login.class.php");

$result = new Login("meme", "meme");
?>

Recommended Answers

All 2 Replies

I usually like to take the time and explain why its wrong but I'm lazy today so I'll just fix it.

<?php
class DBconnection {
  private $host="localhost";
  private $user="root";
  private $password=null;
  private $database="classTest";

  private $conn;
  private $db;

  private static $instance = null;

  private function __construct() {
    $this->conn=mysql_connect($this->host, $this->user, $this->password);
    $this->db=mysql_select_db($this->database);
  }

  public static getInstance()
  {
    if(self::$instance === null) {
      self::$instance = new self;
    }
    return self::$instance;
  }

  public function getConnection()
  {
    return $this->conn;
  }
}


// ExecuteSql should NOT extend DBconnection, it just doesn't make any damn sense
class ExecuteSql {
  public static function executeSql($sql) {
    $db = DBconnection::getInstance();
    $runSql=mysql_query($sql, $db->getConnection()) or die ("ERROR: Database connection error");

    if (@mysql_num_rows($runSql) == 1) {
      return "VALID";
    } else {
      return "INVALID";
    }
  }
}


// Once again, why the hell is this extending DBconnection?
class Login {
  public function __construct($username, $password) {
    // NOOOO!!!!!!!!!!!!   DBconnection::__construct();
    echo ExecuteSql::executeSql("SELECT id FROM login WHERE username='$username' AND password='$password'");
  }
}

If you want an explanation ask, otherwise just continue on not reading documentation like you were before.

commented: Nice use of the singleton method. +4

Ok thank you. Solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.