-2

When i prepare the $sql, i got error "Notice: Undefined variable: obj in.."

member.php

<?php   
require_once('database.php');
$obj = new Database;
$obj->getConnection();
class Member{

private $username;
private $userpassword;

public function regist(){

if(isset($_POST['action'])&&$_POST['action']=='insert'){
try{

    $sql = "INSERT INTO userData(username,password)VALUES(:username,:password)";
    $stmh = $obj->prepare($sql);//the error occour here
    $stmh -> bindValue('username',$_POST['username'],PDO::PARAM_STR);
    $stmh -> bindValue('password',password_hash($_POST['userpassword'],PASSWORD_DEFAULT),PDO::PARAM_STR);
    $stmh -> execute();

    echo "登録しました<br>";
        }catch(PDOExcetion $e){
            echo "error ".$e->getMessage();
        }
    }

}

public function login(){
    $sql = "SELECT * FROM userData WHERE username=:usrname";
    $stmh =$obj->prepare($sql);//the error occour here
    $stmh ->bindValue(':usrname',$_POST['username'],PDO::PARAM_STR);
    $stmh->execute();
    $row = $stmh->fetch(PDO::FETCH_ASSOC);
    if($row['username'] == false){
        echo"login fail";
        exit;
    }
    $hash = $row['password'];
    if (false === password_verify($_POST['userpassword'],$hash)){
        echo"login fail";
        exit;
    }
   }
}

database.php i just create Database class, i want to use $pdo which is protected, so i create getConnection() function and return the $pdo. So what i think is i just create a object like $obj=new Database(); $obj->getConnection(); then i can access $pdo variable.But it doesn't work. The error occur at above "member.php". please help

<?php
class Database{

protected $pdo;

public function __construct()
{
    $this->db_connect();
}

private function db_connect()
{
    $servername = "localhost";
    $username = "abc";
    $password = "abc";
    $dbname = "mydb";
    try
    {
      $this->pdo = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
      $this->pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
      $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
    }catch(PDOException $e)
        {
        die("error connection ".$e->getMessage());
        }
}
public function getConnection()
{   
    return $this->pdo;
}   
  }
  ?>
Rajesh Patel
  • 1,946
  • 16
  • 20
Tuang
  • 15
  • 10
  • 3
    Well the variable is out of scope in the method. See: http://stackoverflow.com/q/16959576/3933332 – Rizier123 Nov 03 '16 at 01:47
  • 1
    define variable inside the class – Rajesh Patel Nov 03 '16 at 01:48
  • 1
    you should really be posting the full error message however both cases of `$obj` in your member class is undefined because of variable scope. `$obj` has been declared outside your class so for anything the class it doesn't exist – Memor-X Nov 03 '16 at 01:48
  • @Rizier123 looks like a dupe to me. – Funk Forty Niner Nov 03 '16 at 02:00
  • @Memor-x thanks, now i got this error what does it's means? Parse error: syntax error, unexpected '$obj' (T_VARIABLE), expecting function (T_FUNCTION) in C:\xampp\New folder\htdocs\todo\member.php on line 8 – Tuang Nov 03 '16 at 02:08

1 Answers1

0

Add global $obj in the function before any thing.