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.

Recommended Answers

All 3 Replies

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;

        }

    }

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.