0

I have two php classes, in two files.

....db.php ....sql.php

db contains:

class DataBase {

private $link;
private $host;
private $username;
private $password;
private $database;

public function __construct() {
    print'<br>in constructor DB!<br>';
    $this->host     = 'xxxx';
    $this->username = 'xxxx';
    $this->password = 'xxxx';
    $this->database = 'xxxx';
    $this->connect();
}

public function connect() {
    $this->link = mysql_connect($this->host,$this->username,$this->password);
    if (!$this->link)   die('Could not connect: ' . mysql_error());
    mysql_select_db($this->database, $this->link);
}

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

}

sql.php contains :

include 'db.php';

class SQL {

    public $dataBase;
    public $con;

    public function __construct() {
        $dataBase = new DataBase; 
        print 'database -> '.$dataBase; 
        $con = $dataBase->getConnection(); 
    }

    public function login($email, $pass) {
        $sql = "SELECT * FROM clienti WHERE email='".$email."' and pass= md5('".$pass."')";
        $result = mysql_query($sql, $con) or die(mysql_error());
        $row = mysql_fetch_assoc($result);
        if(!$row) return 'Email or password is incorrect.';
        return '';
    }
}

in login.php

<?php
include 'include/sql.php';

$email  = $_GET['email'];
$pass= $_GET['pass'];

$error  = '';

if($email == '' || $pass== '') {
    $error = 'Email or password is empty.';
}
else {
    $sql = new SQL(); //if I print inside constructor, $con variable, is something like #3 resources
    echo $sql->con;  // but here is empty   ???
    $sql->login($email, $pass);
    $sql->close();
}   

What I am doing wrong?
The includes are ok, I tested them. But why variables of sql Object are empty??? thank you.

tziuka
  • 545
  • 1
  • 6
  • 23
  • 1
    Unless you've defined a `__toString()` method on `DataBase`, I'd `var_dump` it rather then `echo`. And, you do have full error reporting on? – Wrikken Dec 05 '14 at 15:48
  • Is it saying `try to call getConnection on a non object` ? – Pete_Gore Dec 05 '14 at 15:48
  • 2
    Such a shame using a class along with a deprecated library and MD5 for password storage. All old and considered unsafe to use by today's standards. You're best using prepared statements and `password_hash()`. Visit and use http://php.net/manual/en/function.error-reporting.php add it to the top of your files to signal errors found. Chances are, you'll get a "deprecation" warning. Once `mysql_` has been deleted from your server, you will be forced to do a complete rewrite. – Funk Forty Niner Dec 05 '14 at 15:51
  • @Wrikken I have error on Sql login function second line whit this error : Call to a member function getConnection() on a non-object in /home...sql.php on line 15 – tziuka Dec 05 '14 at 15:52
  • I can't see how you are getting that error with your current code shown. Are you perhaps calling `$this->dataBase->getConnection();`? Because you have not set the class property `private $dataBase`, you only initialized a local variable in the constructor. – Flosculus Dec 05 '14 at 15:52
  • Thanks @Fred-ii-, I do this for school project and I am not php programmer. I will look at that link you give me. – tziuka Dec 05 '14 at 15:53
  • You're welcome. Even though it's a school project, security remains a real world issue. – Funk Forty Niner Dec 05 '14 at 15:55
  • mysql_query() expects parameter 2 to be resource, null given in ...this is the error in line 16-> second of login function – tziuka Dec 05 '14 at 15:58
  • @tziuka Take a look here: http://stackoverflow.com/q/25318487/3933332 – Rizier123 Dec 05 '14 at 15:59
  • 1
    I'm thinking that, your teacher has given you this assignment to figure out how you can make it work; just as I suspect that you are using a MySQL library that is deprecated. It's time to use either "mysqli" or PDO. In doing so, may very well resolve the issue. – Funk Forty Niner Dec 05 '14 at 16:06

3 Answers3

3

$con is a property of the class not a variable, you refer to it by using keyword $this:

$con = $dataBase->getConnection(); //WRONG

change to:

$this->con = $dataBase->getConnection(); //CORRECT
meda
  • 44,540
  • 14
  • 88
  • 122
1

You forgot () So try this:

 $dataBase = new DataBase();

Also it could be that you get following error:

Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead

So i would recommend you to rewrite your code to PDO or mysqli

BTW:

For error reporting use:

<?php
    ini_set("display_errors", 1);
    error_reporting(E_ALL);
?>
Rizier123
  • 57,440
  • 16
  • 89
  • 140
  • I tried like this, is no difference... – tziuka Dec 05 '14 at 15:49
  • @tziuka What do you expect the output would be? – Rizier123 Dec 05 '14 at 15:51
  • I expected to be 'object DataBase' or something like that.. but I understud I have to override toString method. The main problem is in SQL second line of login function. I have this error : mysql_query() expects parameter 2 to be resource, null given – tziuka Dec 05 '14 at 15:56
0

You have $con listed as private, so you can't access it from outside the class, like $sql->con. If you need to use it like $sql->con, change this in SQL.php:

private $con;

to this:

public $con;

Based on one of your comments though, your issue may simply be that you need to use $this->con instead of just $con in the methods of your SQL class.

mopo922
  • 6,186
  • 3
  • 27
  • 31