我正在尝试为我的网站制作一个OOP登录系统,并且在连接到MS SQL数据库时遇到了麻烦,所以真的陷入了第一个障碍!
我正在使用SQLSRV驱动程序,并且不能安装PDO。我收到以下错误,我理解这意味着没有建立连接并且db_connection变量为null:
致命错误在第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.";
}
}
}
我尝试在OOP之外以完全相同的方式建立连接,它工作得很好。我没看到出了什么问题!感谢您的帮助,非常感谢!
答案 0 :(得分:0)
sqlsrv
没有名为set_charset
的功能。相反,可以在连接参数数组中设置此选项,如:
$connectionInfo = array(
"Database" => $DB,
"UID" => $User,
"PWD" => $Pass,
"CharacterSet" => "UTF-8"
);
检查更改为:
...
if (!$this->db_connection) {
// You can modify here however you like
$this->errors[] = print_r(sqlsrv_errors(), true);
}
您还应该删除第二项检查,因为我们正在使用上面的代码:
// if no connection errors (= working database connection)
if (!$this->db_connection->connect_errno) {
...
您的代码中似乎有更多错误,例如sqlsrv
也没有类似ms_escape_string
的功能。您应该再次查看official documentation。
答案 1 :(得分:0)
使用sqlsrv_errors()更清楚地了解问题。我猜你没有安装SQL Native Client。