0

i have this class for connect to MySql database and create/insert user but when work with this i see error.

My Auth Class is:

    class AuthDB {
        private $db;

        public function __construct() {
            $db = new Dbaccess();

    if (!$db->connect ( DB_SERVER, DB_USER, DB_PASS, DB_NAME ) ) {
     if (mysql_error() == '') 
      $database_incorrect = 'Database name is incorrect or doesn\'t exist'; 
     else 
      $database_incorrect = 'ERROR: ';
     echo $database_incorrect . mysql_error ();
    }
        }

        public function createUser($email, $password, $salt, $verification) {

            $ver = 0;
            $act = 1;
            $adm = 0;

            $MYSQLDB = "INSERT INTO tbUsers (email, password, user_salt, is_verified, is_active, is_admin, verification_code) "
            . "VALUES ('1', '1', '1', '1', '1', '1', '1')"; //test value

            $r2 = $db->query ($MYSQLDB) or error ('Critical Error', mysql_error () ); // <<<LINE 30 ERROR HERE

            if ($r2 > 0) {
                return true;
            }

            return false;
        }
   }

My Dbaccess class Is:

class Dbaccess
{
var $q_array = array();
var $db_id;
var $query;
var $counter = 0;
var $timecounter = 0;
var $query_res;

function connect ($host, $login, $password, $db)
{
$this->host = $host;
$this->login = $login;
$this->password = $password;
$this->db = $db;
$this->db_id = @mysql_connect($this->host, $this->login, $this->password);
if ($this->db_id)
{
$db_select = @mysql_select_db($this->db);
$this->query("SET NAMES 'UTF8'");
if (!$db_select)
{
@mysql_close($this->db_id);
$this->db_id = $db_select;
}
else
return $this->db_id;
}
else
return false;
}
function close()
{
if($this->db_id)
{
if($this->query)
{
@mysql_free_result($this->query);
}
$result = @mysql_close($this->db_id);
return $result;
}
else {
return false;
}}
function query ($query)
    //
    // $db->query("QUERY");

    {

        unset($this->query_res);

        if($query != "")

        {

            $sql_start = explode(' ', microtime());

            $this->query_res = @mysql_query($query, $this->db_id);

            $sql_stop = explode(' ', microtime());
            $sql_time = $sql_stop[0] - $sql_start[0];
            $sql_time+= $sql_stop[1] - $sql_start[1];

            $this->timecounter+= round($sql_time, 5);
            $this->counter++;

        }

        if($this->query_res)

        {
            unset($this->q_array[$this->query_res]);
            return $this->query_res;
        }
        else
        {
            return false;
        }
    }
}

Now i see this error when send data :

Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\test\classes\Auth.php on line 30

How to Fix This Error? Where is my mistake?

B.B King
  • 111
  • 2
  • 3
  • 7
  • do not user mysql_* function ! instead user mysqli_* or PDO – Ali Akbar Azizi Mar 31 '13 at 11:07
  • [Please, don't use `mysql_*` functions](http://stackoverflow.com/q/12859942/1190388) in new code. They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the red box? Learn about prepared statements instead, and use [tag:PDO] or [tag:MySQLi]. – hjpotter92 Mar 31 '13 at 11:07
  • In AuthDB class, replace $db = new Dbaccess(); with $this->db = new Dbaccess(); – Arvind Mar 31 '13 at 11:14

1 Answers1

2

You want this in your __construct:

public function __construct() {
  $this->db = new Dbaccess();

And when you query, you want:

$this->db->query($MYSQLDB);

When you just use the variable $db, it only exists within the scope of that method. But when you use class properties it exists within the scope of the entire class.

Michael
  • 11,912
  • 6
  • 49
  • 64