我是使用PHP的MS-SQL新手。 我试图将会话保存到数据库,但在MS-SQL中。 当我运行php我得到错误。
sqlsrv_query()期望参数1是资源,布尔值。
这是我的代码。
<?php
class SQLSrv_Session
{
const DB_HOST= "1DB_HOST";
const DB_PASS= "DB_PASS";
const DB_USER= "DB_USER";
const DB_NAME= "DB_NAME";
const DB_SESSION_TABLE="DB_SESSION_TABLE";
/**
* Database connection resource
*
* @var resource
*/
private static $_sess_db;
/**
* Escape string for mssql query
*
* @param string $data
* @return string
*/
private static function sqlsrv_escape($data) {
return str_replace("'","''",$data);
}
/**
* Open the session
*
* @return bool
*/
public static function open() {
$connectionInfo= array (
'UID' => self::DB_USER,
'PWD' => self::DB_PASS,
'Database' => self::DB_NAME
);
self::$_sess_db = sqlsrv_connect(self::DB_HOST,$connectionInfo);
return self::$_sess_db;
}
/**
* Close the session
*
* @return boolean
*/
public static function close() {
return sqlsrv_close(self::$_sess_db);
}
/**
* Read the session
*
* @param string $id
* @return string
*/
public static function read($id) {
$sql = 'SELECT SCS_DATA FROM '. self::DB_SESSION_TABLE .' '.
'WHERE SCS_ID = \''.self::sqlsrv_escape($id).'\'';
$stmt= sqlsrv_query(self::$_sess_db, $sql);
if (!empty($stmt)) {
$row= sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC);
return $row['SCS_DATA'];
}
return '';
}
/**
* Write the session
*
* @param string $id
* @param string $value
* @return boolean
*/
public static function write($id, $value) {
echo 'aaaaaaa';
$sql= 'UPDATE '.self::DB_SESSION_TABLE.' SET SCS_DATA=?,SCS_ACCESS=? WHERE SCS_ID=?';
$params = array(111112, 3333322, 12222221);
$stmt= sqlsrv_query(self::$_sess_db, $sql, $params);
print_r($params);
if (!empty($stmt)) {
$result= sqlsrv_rows_affected($stmt);
if ($result>0) {
echo 'eeew';
return true;
}else{
echo $result;
}
}
$sql= 'INSERT INTO '.self::DB_SESSION_TABLE.' (SCS_DATA,SCS_ACCESS, SCS_ID) VALUES (?,?,?)';
$stmt= sqlsrv_query(self::$_sess_db, $sql, $params);
print_r($params);
if (!empty($stmt)) {
$result= sqlsrv_rows_affected($stmt);
if ($result>0) {
return true;
echo 'yyyyyy';
}else{
echo $result;
}
}
return false;
}
/**
* Destroy the session
*
* @param string $id
* @return boolean
*/
public static function destroy($id) {
$sql = 'DELETE FROM '.self::DB_SESSION_TABLE.' WHERE SCS_ID = ?';
$params= array(self::sqlsrv_escape($id));
$stmt= sqlsrv_query(self::$_sess_db,$sql,$params);
return ($stmt!==false);
}
/**
* Garbage Collector
*
* @param integer $max
* @return boolean
*/
public static function gc($max) {
$sql = 'DELETE FROM '.self::DB_SESSION_TABLE.' WHERE SCS_ACCESS < ?';
$params= array(array(date("Y-m-d H:i:s",time() - $max)));
$stmt= sqlsrv_query(self::$_sess_db, $sql, $params);
return ($stmt!==false);
}
}
// to prevent problem of session write after close as PHP 5.0.5
register_shutdown_function('session_write_close');
ini_set('session.save_handler', 'user');
session_set_save_handler(array('SQLSrv_Session', 'open'),
array('SQLSrv_Session', 'close'),
array('SQLSrv_Session', 'read'),
array('SQLSrv_Session', 'write'),
array('SQLSrv_Session', 'destroy'),
array('SQLSrv_Session', 'gc'));
// display PHP error
error_reporting(E_ALL);
ini_set("display_errors", 1);
// set the probability of garbage collector to 1
ini_set('session.gc_probability', 100);
ini_set('session.gc_divisor', 100);
session_start();
$_SESSION ['lll']= 'pppp';
我收到第55,121,74,86,44行的错误。
这是带有错误的行。
$ stmt = sqlsrv_query(self :: $ _ sess_db,$ sql);
$ stmt = sqlsrv_query(self :: $ _ sess_db,$ sql,$ params);
$ stmt = sqlsrv_query(self :: $ _ sess_db,$ sql,$ params);
$ stmt = sqlsrv_query(self :: $ _ sess_db,$ sql,$ params);
sqlsrv_close(自:: $ _ sess_db);
如果有人请找到wirer,错误就是sqlsrv_query。 谢谢