简体   繁体   English

在没有PDO的OOP中使用SQLSRV驱动程序进行数据库连接

[英]Database Connection with SQLSRV drivers in OOP without PDO

I am trying to make an OOP login system for my website and am having real trouble connecting to the MS SQL database, so falling at the first hurdle really! 我正在尝试为我的网站制作一个OOP登录系统,并且在连接到MS SQL数据库时遇到了真正的麻烦,因此,实际上是第一个障碍!

I am using the SQLSRV drivers and installing PDO is not an option. 我正在使用SQLSRV驱动程序,并且无法安装PDO。 I am getting the following error which I understand to mean that the connection is not being established and the db_connection variable is null: 我收到以下错误,我理解这意味着未建立连接并且db_connection变量为null:

Fatal Error Call to a member function set_charset() on a non-object in C:\\xxxx\\auth.php on line 79 致命错误在第79行的C:\\ xxxx \\ auth.php中的非对象上调用成员函数set_charset()

class Auth
{

/**
 *  @var string - database connection
 */
private $db_connection = null;

/**
 * @var array - collection of error messages
 */
public $errors = array();

/**
 * @var array - collection of success / neutral messages
 */
public $messages = array();


public function __construct()
{
    // create/read session, absolutely necessary
    session_start();

    // check the possible login actions:
    // if user tried to log out (happen when user clicks logout button)
    if (isset($_GET["logout"])) {
        $this->doLogout();
    }
    // login via post data (if user just submitted a login form)
    elseif (isset($_POST["auth"])) {
        $this->doauthWithPostData();
    }
}



// AUTHENTICATE
private function doauthWithPostData()
{

    $serverName = "%server%";
    $User = "%user%";
    $Pass = "%pwd%";
    $DB = "%dbname%";


    $connectionInfo = array( "Database"=>$DB, "UID"=>$User, "PWD"=>$Pass);


    // check login form contents
    if (empty($_POST['usr'])) {
        $this->errors[] = "Username field was empty.";
    } elseif (empty($_POST['usr_pwd'])) {
        $this->errors[] = "Password field was empty.";
    } elseif (!empty($_POST['usr']) && !empty($_POST['usr_pwd'])) {

        // create a database connection, using the constants from db.php (loaded in index.php)
        $this->db_connection = sqlsrv_connect( $serverName, $connectionInfo);
        //$this->db_connection = new mssql_connect(constant(DB_HOST), constant(DB_USER), constant(DB_PASS));

        // change character set to utf8 and check it
         if (!$this->db_connection->set_charset("utf8")) {
            $this->errors[] = $this->db_connection->error;
         }

        // if no connection errors (= working database connection)
         if (!$this->db_connection->connect_errno) {

        // escape the POST stuff
             $user_name = $this->db_connection->ms_escape_string($_POST['user_name']);

           // database query, get all the info of the selected organisation
            $qry = "SELECT USR, PASSWORD
                    FROM USERS
                    WHERE USR = '" . $_POST['usr'] . "';";
            $result_of_auth_check = $this->db_connection->query($qry);

            // if this org exists
            if ($result_of_auth_check->num_rows == 1) {

                // get result row (as an object)
                $result_row = $result_of_auth_check->fetch_object();

                // password_verify() function to check if the provided password fits
                // the hash of that user's password
                if (password_verify($_POST['usr_pwd'], $result_row->user_password_hash)) {

                    // write user data into PHP SESSION
                    $_SESSION['user_auth_status'] = $_POST['usr'];

                } else {
                    $this->errors[] = "Wrong password. Try again.";
                }
            } else {
                $this->errors[] = "This user does not exist.";
            }
        } else {
            $this->errors[] = "There was a problem connecting to the database.";
        }
    }
}

I have tried making the connection in exactly the same way outside of OOP and it works just fine. 我尝试过在OOP之外以完全相同的方式进行连接,并且效果很好。 I don't see what is going wrong! 我看不出怎么了! Thanks for any help, really appreciated! 感谢您的帮助,非常感谢!

sqlsrv doesn't have a function called set_charset . sqlsrv没有名为set_charset的函数。 Instead, this option can be set in connection parameters array like: 相反,可以在连接参数数组中设置此选项,例如:

$connectionInfo = array(
   "Database"     => $DB,
   "UID"          => $User,
   "PWD"          => $Pass,
   "CharacterSet" => "UTF-8"
);

And checking changes to: 并检查对以下内容的更改:

...
if (!$this->db_connection) {
   // You can modify here however you like
   $this->errors[] = print_r(sqlsrv_errors(), true);
}

You should also remove second check, because we are doing it with the code above: 您还应该删除第二项检查,因为我们正在使用上面的代码进行检查:

// if no connection errors (= working database connection) if (!$this->db_connection->connect_errno) { ... // if no connection errors (= working database connection) if (!$this->db_connection->connect_errno) { ...

There seems more error in your code like sqlsrv doesn't have a function like ms_escape_string also. 似乎在你的代码更错误,如sqlsrv没有像函数ms_escape_string也。 You should check official documentation more. 您应该查看更多官方文档

Use sqlsrv_errors() to get a clearer idea of the problem. 使用sqlsrv_errors()可以更清楚地了解问题。 I'd guess you don't have the SQL Native Client installed. 我猜您没有安装SQL Native Client。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM