将PHP类从mssql_u更新为sqlsrv_
我们刚刚在服务器上更新了PHP,大部分情况下一切正常,但不幸的是mssql_uu函数不再受支持。我已尝试更新我们以前的课程:将PHP类从mssql_u更新为sqlsrv_,php,sql-server,Php,Sql Server,我们刚刚在服务器上更新了PHP,大部分情况下一切正常,但不幸的是mssql_uu函数不再受支持。我已尝试更新我们以前的课程: $connection['server'] = 'server, port'; $connection['user'] = 'user'; $connection['pass'] = 'pass'; $connection['db'] = 'db'; class mssqlClass { function connect($dbhost = NULL){
$connection['server'] = 'server, port';
$connection['user'] = 'user';
$connection['pass'] = 'pass';
$connection['db'] = 'db';
class mssqlClass {
function connect($dbhost = NULL){
global $connection;
if(! ISSET ($dbconnect)){
$dbconnect = mssql_Connect($connection['server'], $connection['user'], $connection['pass'], true);
}
if(! $dbconnect){
return 'Failed to Connect to Host';
}else{
$select = mssql_select_db($connection['db'], $dbconnect);
if(! $select){
return 'Failed to select Database';
}else{
return $dbconnect;
}
}
}
function getData ($query){
$this->data_array = array();
$result = mssql_query($query);
while ($row = mssql_fetch_assoc($result)) {
$this->data_array[] = $row;
}
$m = $this->data_array;
return $m;
}
function query($query){
$result = mssql_query($query) or die("Query didn't work");
}
}
为了与sqlsrv兼容,虽然我可以连接到数据库而没有任何问题,但它不会返回任何数据!:
class mssqlClass {
function connect($database = 'Db') {
$mssql_server = 'server';
$mssql_data = array("UID" => 'uid',
"PWD" => 'pwd',
"Database" => $database);
if(! ISSET ($dbconnect)){
$dbconnect = sqlsrv_connect($mssql_server, $mssql_data);
}
if(! $dbconnect){
return 'Failed to connect to host';
}
}
function getData ($query) {
$result = sqlsrv_query($db->connect, $query);
while ($row = sqlsrv_fetch_array($result)) {
$this->data_array[] = $row;
}
$m = $this->data_array;
return $m;
}
function query($query) {
$result = sqlsrv_query($query) or die("Query didn't work.");
}
}
注:示例用法如下:
$db = new mssqlClass();
$conn = $db->connect('DATABASE');
$query = "SELECT * FROM Table";
$result= $db->getData($query);
很抱歉,代码太多了-如果更简单的话,我可以将其编辑为getData函数?谢谢 代码看起来不错。唯一可能是您的问题是sqlsrv_fetch_数组可能需要第二个参数,例如:
sqlsrv_fetch_array( $result, SQLSRV_FETCH_ASSOC)
因为默认情况下,它返回两个格式不同的数组。如果您可以通过名称访问属性,它将不返回任何内容,但不会失败
像这样的。因为您实际上使用以下方法的返回值调用sqlsrv_查询方法。在原始版本中,您没有返回连接资源
function connect($database = 'Db') {
$mssql_server = 'gc-hr01';
$mssql_data = array("UID" => 'uid',
"PWD" => 'pwd',
"Database" => $database);
$dbconnect = sqlsrv_connect($mssql_server, $mssql_data);
if(! $dbconnect){
return 'Failed to connect to host';
}
return $dbconnect;
}
提前
function getData ($query) {
$result = sqlsrv_query($db->connect(), $query);
while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {
$this->data_array[] = $row;
}
$m = $this->data_array;
return $m;
}
function query($query) {
$result = sqlsrv_query($query) or die("Query didn't work.");
}
我在你们班上做了一些改变:
<?php
class mssqlClass {
protected $connection = null;
public function connect($database = 'Db') {
// we don't need to connect twice
if ( $this->connection ) {
return;
}
// data for making connection
$mssql_server = 'gc-hr01';
$mssql_data = array("UID" => 'uid',
"PWD" => 'pwd',
"Database" => $database);
// try to connect
$this->connection = sqlsrv_connect($mssql_server, $mssql_data);
if(! $dbconnect){
return 'Failed to connect to host';
}
}
public function getData ($query) {
// reset results; is this really needed as object's variable? Can't it be just local function's variable??
$this->data_array = array();
$result = $this->query($this->connection, $query);
while ($row = sqlsrv_fetch_array($result)) {
$this->data_array[] = $row;
}
return $this->data_array;
}
public function query($query) {
$result = sqlsrv_query($query) or die("Query didn't work..");
}
}
我在试图为sqlsrv命令找到一条通往其他人的包装类的捷径时找到了这个页面。由于这还不完全,我很快就编译了自己的。请不要认为这是生产代码,您应该一次又一次地测试,但它可能会帮助某人快速摆脱困境
class db {
protected $connection = null;
private $debug = true;
private $stmt = null;
private $insertid = null;
private $affectedrows = null;
public function db($host = '.',$database,$username,$password) {
// we don't need to connect twice
if ( $this->connection ) {
return;
}
sqlsrv_configure('WarningsReturnAsErrors', 0);
// data for making connection
$sqlsvr_details = array( 'UID' => $username,
'PWD' => $password,
'Database' => $database,
'CharacterSet' => 'UTF-8'
);
// try to connect
$this->connection = sqlsrv_connect($host, $sqlsvr_details);
if($this->connection == false){
$this->debug('Failed to connect to host: '.$this->errors(),true);
return false;
}else{
return true;
}
}
public function query($query) {
return new resultset($query,$this->connection,$this->debug);
}
private function debug ($message,$hard = false){
if ($this->debug){
if ($hard){
die($message);
}else{
echo $message;
}
}
return true;
}
public function delete($query){
return $this->update($query);
}
public function update($query){
//Not happy with this
$this->affectedrows = null;
$this->insertid = null;
$this->stmt = sqlsrv_query($this->connection, $query);
if (!$this->stmt){
$this->debug('SQL Query Failed: '.$query.' '.$this->errors(),true);
return false;
}else{
$this->affectedrows = @sqlsrv_rows_affected($this->stmt);
return $this;
}
}
public function insert($query){
//Not happy with this
$this->affectedrows = null;
$this->insertid = null;
$this->stmt = sqlsrv_query($this->connection, $query);
if (!$this->stmt){
$this->debug('SQL Query Failed: '.$query.' '.$this->errors(),true);
return false;
}else{
//Get the last insert ID and store it on here
$this->insertid = $this->query('select @@IDENTITY as insert_id')->asObject()->insert_id;
return $this;
}
}
public function insert_id(){
return $this->insertid;
}
public function affected_rows(){
return $this->affectedrows;
}
private function errors(){
return print_r( sqlsrv_errors(SQLSRV_ERR_ERRORS), true);
}
}
class resultset implements Countable,Iterator {
private $result = null;
private $connection = null;
private $debug = false;
private $internal_pointer = 0;
private $data = array();
public function resultset($query,$link,$debug = false){
$this->connection = $link;
$this->debug = $debug;
$this->result = sqlsrv_query($this->connection, $query, array(), array('Scrollable' => SQLSRV_CURSOR_STATIC));
if ($this->result == false){
$this->debug('Query Failed: '.$query.' '.$this->errors(),true);
return false;
}else{
return $this;
}
}
public function asObject($step = true){
$object = sqlsrv_fetch_object($this->result,NULL,NULL,SQLSRV_SCROLL_ABSOLUTE,$this->internal_pointer);
if (! $object){
return false;
}else{
if ($step) $this->internal_pointer++;
return $object;
}
}
public function num_rows() {
return sqlsrv_num_rows($this->result);
}
public function free(){
$this->internal_pointer = 0;
if (is_resource($this->result)){
sqlsrv_free_stmt($this->result);
}
}
public function __destory(){
$this->free();
}
//Countable Function
public function count(){
return $this->num_rows();
}
//Iteration Functions
public function rewind(){
$this->internal_pointer = 0;
}
public function current(){
return $this->asObject(false);
}
public function key(){
return $this->internal_pointer;
}
public function next(){
$this->internal_pointer++;
}
public function valid(){
return $this->internal_pointer <= $this->num_rows();
}
//============================================
private function debug ($message,$hard = false){
if ($this->debug){
if ($hard){
die($message);
}else{
echo $message;
}
}
return true;
}
private function errors(){
return print_r( sqlsrv_errors(SQLSRV_ERR_ERRORS), true);
}
}
classdb{
受保护的$connection=null;
private$debug=true;
私有$stmt=null;
private$insertid=null;
private$affectedrows=null;
公共函数数据库($host='.',$database,$username,$password){
//我们不需要连接两次
如果($this->connection){
回来
}
sqlsrv_configure('warningreturnaserrors',0);
//用于建立连接的数据
$sqlsvr_details=数组('UID'=>$username,
“PWD”=>$password,
“数据库”=>$Database,
'字符集'=>'UTF-8'
);
//尝试连接
$this->connection=sqlsrv\u connect($host,$sqlsvr\u details);
如果($this->connection==false){
$this->debug('未能连接到主机:'。$this->errors(),true);
返回false;
}否则{
返回true;
}
}
公共函数查询($query){
返回新的结果集($query、$this->connection、$this->debug);
}
专用函数调试($message,$hard=false){
如果($this->debug){
如果($hard){
死亡($信息);
}否则{
回声$信息;
}
}
返回true;
}
公共函数删除($query){
返回$this->update($query);
}
公共功能更新($query){
//对此不满意
$this->affectedrows=null;
$this->insertid=null;
$this->stmt=sqlsrv\u query($this->connection,$query);
如果(!$this->stmt){
$this->debug('SQL查询失败:'.$Query'.$this->errors(),true);
返回false;
}否则{
$this->affectedrows=@sqlsrv\u rows\u impacted($this->stmt);
退还$this;
}
}
公共函数插入($query){
//对此不满意
$this->affectedrows=null;
$this->insertid=null;
$this->stmt=sqlsrv\u query($this->connection,$query);
如果(!$this->stmt){
$this->debug('SQL查询失败:'.$Query'.$this->errors(),true);
返回false;
}否则{
//获取最后一个插入ID并将其存储在此处
$this->insertid=$this->query('select@@IDENTITY as insert_id')->asObject()->insert_id;
退还$this;
}
}
公共函数insert_id(){
返回$this->insertid;
}
受影响的公共功能_行(){
返回$this->affectedrows;
}
私有函数错误(){
返回print_r(sqlsrv_errors(sqlsrv_error_errors)),true;
}
}
类resultset实现可数迭代器{
private$result=null;
private$connection=null;
private$debug=false;
私有$internal_指针=0;
private$data=array();
公共函数resultset($query,$link,$debug=false){
$this->connection=$link;
$this->debug=$debug;
$this->result=sqlsrv\u query($this->connection,$query,array(),array('Scrollable'=>sqlsrv\u CURSOR\u STATIC));
如果($this->result==false){
$this->debug('查询失败:'.$Query'.$this->errors(),true);
返回false;
}否则{
退还$this;
}
}
公共函数asObject($step=true){
$object=sqlsrv\u fetch\u object($this->result,NULL,NULL,sqlsrv\u SCROLL\u ABSOLUTE,$this->internal\u指针);
if(!$object){
返回false;
}否则{
如果($step)$this->internal_pointer++;
返回$object;
}
}
公共函数num_rows(){
返回sqlsrv_num_行($this->result);
}
公共功能自由(){
$this->internal_pointer=0;
如果(是_资源($this->result)){
sqlsrv_free_stmt($this->result);
}
}
公共职能{
$this->free();
}
//可数函数
公共功能计数(){
返回$this->num_rows();
}
//迭代函数
公共函数倒带(){
$this->internal_pointer=0;
}
公共职能(当前){
返回$this->asObject(false);
}
公共函数密钥(){
返回$this->internal_指针;
}
公共职能下一步(){
$this->internal_pointer++;
}
公共函数有效(){
返回$this->internal_pointer num_rows();
}
//============================================
专用函数调试($message,$hard=false){
如果($this->debug){
如果($hard){
死亡($信息);
}否则{
欧共体