0

I know this has been asked many times before so I apologize but here's the php file that's generating the error on the "if($stmt = $this->conn->prepare($query))" line:

<?php

require_once 'includes/constants.php';

class Mssql {
    private $conn;

    function __construct() {
        $dbServer = DB_SERVER;
        $dbName = DB_NAME;
        $conn = new PDO( "sqlsrv:Server=$dbServer;Database=$dbName", NULL, NULL);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
        if ( $conn === false ) {
            echo "Could not connect.\n";
            die;
        }
    }

    function verify_Username_and_Pwd($un, $pwd) {
        $query = "SELECT * FROM users WHERE username = ? AND password = ? LIMIT 1";
        if($stmt = $this->conn->prepare($query)) {
            $stmt->bind_param('ss', $un, $pwd);
            $stmt->execute();

            if($stmt->fetch()) {
                $stmt->close();
                return true;
            }
        }
    }
}

And here's the constants.php file that's required:

<?php

// Define constants here

define("DB_SERVER", "localhost");
define("DB_NAME", "testdb");

I could really use some help in solving this, thanks.

2
Contributors
3
Replies
12
Views
5 Years
Discussion Span
Last Post by spowel4
1

I think you have to change $conn to $this->conn in __construct ?

function __construct() {

        $dbServer = DB_SERVER;

        $dbName = DB_NAME;

        $this->conn = new PDO( "sqlsrv:Server=$dbServer;Database=$dbName", NULL, NULL);

        $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);

        if ( $this->conn === false ) {

            echo "Could not connect.\n";

            die;

        }

    }
0

I tried that but it didn't work; thanks for trying to help though.

0

Update: kutchbhi, you were correct. Changing the syntax to $this->conn did fix it after all, thanks for the tip!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.