0

I have two php files, one is a file for a PDO Connection and one is a file in which a prepared statement is submitted on this connection:

PDO Connection:

class Connection {

protected $user = "ni873420_2sql1";
protected $pass = "#";
public $pdo_log;

public function __construct() {

        try {

            $pdo_log = new PDO('mysql:host=localhost;dbname=ni873420_2sql1', 
                               $this->user, $this->pass);

        }
        catch (Exception $e) {

            die($e);

        }
    }
}

And this is the function where I create the prepared statement:

    public static function getInfobyEmailUsername($argemailusername) {

    $get_info_query = "SELECT * FROM user WHERE email = ? OR username = ?";

    $conn = new Connection();
    $getinfostmt = $conn->pdo_log->prepare($get_info_query);
    $getinfostmt->execute(array($argemailusername, $argemailusername));

    $userinfo = $getinfostmt->fetch(PDO::FETCH_ASSOC);

    if (!empty ($userinfo)) {
        return $userinfo;
    } else {
        return false;
    }

    }

But I keep getting the error:

Fatal error: Call to a member function prepare() on null

I cant find a solution anywhere, can anyone help me with this or at least send a link where I can find the solution?

Thank you!

DoubleJ
  • 117
  • 1
  • 11
  • 1
    You never assign the handle to `$conn->pdo_log` but left it to die as local variable in the constructor. – mario Jan 01 '18 at 16:10

1 Answers1

1

In your construct function, use the public variable $pdo_log

  $this->pdo_log = new PDO('mysql:host=localhost;dbname=ni873420_2sql1', 
                           $this->user, $this->pass);
Rotimi
  • 4,783
  • 4
  • 18
  • 27