类中的PHP会话

时间:2013-05-06 12:05:49

标签: php html css class session-variables

<?
session_start();
class DB_MSSQL {
  protected $Host;
  protected $Database;
  protected $User;
  protected $Password;
  protected $Link_ID  = 0;
  protected $Query_ID = 0;
  protected $Record   = array();
  protected $Row      = 0;
  protected $Errno    = 0;
  protected $Error    = "";
  protected $Halt_On_Error = "yes";
  protected $Auto_Free = 1;     
  protected $PConnect  = 0;
    function _construct(){
   $this->Host = $_SESSION['SQLIP'];
     $this->Database = $_SESSION['SQLDB'];
     $this->User = $_SESSION['SQLUSER'];
     $this->Password = $_SESSION['SQLPASS'];
  }

  function DB_MSSQL($query = "") {
    if($query) {
      $this->query($query);
    }
  }
  function connect() {
    if ( 0 == $this->Link_ID ) {
      if(!$this->PConnect) {
        $this->Link_ID = mssql_connect($this->Host, $this->User, $this->Password);
      } else {
        $this->Link_ID = mssql_pconnect($this->Host, $this->User, $this->Password);
      }
      if (!$this->Link_ID)
        $this->connect_failed("connect ($this->Host, $this->User, \$Password) failed");
      else
        if (!mssql_select_db($this->Database, $this->Link_ID)) {
          $this->connect_failed("cannot use database ".$this->Database);
        }
    }
  }
  function connect_failed($message) {
    $this->Halt_On_Error = "yes";
    $this->halt($message);
  }

  function free_result(){
      mssql_free_result($this->Query_ID);
    $this->Query_ID = 0;
  }

  function query($Query_String) 
  {

    /* No empty queries, please, since PHP4 chokes on them. */
    if ($Query_String == "")
      /* The empty query string is passed on from the constructor,
       * when calling the class without a query, e.g. in situations
       * like these: '$db = new DB_Sql_Subclass;'
       */
      return 0;

    if (!$this->Link_ID)
        $this->connect();
//    printf("<br>Debug: query = %s<br>\n", $Query_String);

    $this->Query_ID = mssql_query($Query_String, $this->Link_ID);
    $this->Row = 0;
    if (!$this->Query_ID) {
      $this->Errno = 1;
      $this->Error = "General Error (The MSSQL interface cannot return detailed error messages).";
      $this->halt("Invalid SQL: ");
    }
    return $this->Query_ID;
  }

  function next_record() {

    if ($this->Record = mssql_fetch_row($this->Query_ID)) {
      // add to Record[<key>]
      $count = mssql_num_fields($this->Query_ID);
      for ($i=0; $i<$count; $i++){
        $fieldinfo = mssql_fetch_field($this->Query_ID,$i);
        $this->Record[strtolower($fieldinfo->name)] = $this->Record[$i];
      }
      $this->Row += 1;
      $stat = 1;
    } else {
      if ($this->Auto_Free) {
            $this->free_result();
        }
      $stat = 0;
    }
    return $stat;
  }

  function seek($pos) {
        mssql_data_seek($this->Query_ID,$pos);
    $this->Row = $pos;
  }

  function metadata($table) {
    $count = 0;
    $id    = 0;
    $res   = array();

    $this->connect();
    $id = mssql_query("select * from $table", $this->Link_ID);
    if (!$id) {
      $this->Errno = 1;
      $this->Error = "General Error (The MSSQL interface cannot return detailed error messages).";
      $this->halt("Metadata query failed.");
    }
    $count = mssql_num_fields($id);

    for ($i=0; $i<$count; $i++) {
        $info = mssql_fetch_field($id, $i);
      $res[$i]["table"] = $table;
      $res[$i]["name"]  = $info->name;
      $res[$i]["len"]   = $info->max_length;
      $res[$i]["flags"] = $info->numeric;
    }
    $this->free_result();
    return $res;
  }

  function affected_rows() {
// Not a supported function in PHP3/4.  Chris Johnson, 16May2001.
//    return mssql_affected_rows($this->Query_ID);
    $rsRows = mssql_query("Select @@rowcount as rows", $this->Link_ID);
    if ($rsRows) {       
       return mssql_result($rsRows, 0, "rows");
    }
  }

  function num_rows() {
    return mssql_num_rows($this->Query_ID);
  }

  function num_fields() {
    return mssql_num_fields($this->Query_ID);
  }

  function nf() {
    return $this->num_rows();
  }

  function np() {
    print $this->num_rows();
  }

  function f($Field_Name) {
    return $this->Record[strtolower($Field_Name)];
  }

  function p($Field_Name) {
    print $this->f($Field_Name);
  }

  function halt($msg) {
    if ("no" == $this->Halt_On_Error)
      return;

    $this->haltmsg($msg);

    if ("report" != $this->Halt_On_Error)
      die("Session halted.");
  }

  function haltmsg($msg) {
    printf("<p>Server have a critical error!<br><br><br>We are very sorry for any inconvenience!<br><br>\n", $msg);
    printf("<b>MSSQL Error</b>: %s (%s)</p>\n",
      $this->Errno,
      $this->Error);
  }
}
?>

Faild to connect ..如果我把$ Host; $ Database; $ User; $ Password;手动工作正常。但是与构造函数无法连接。 ($ _SESSION具有正确的值)

但我不知道如何回应$ obj-&gt;主机,密码,用户等。

2 个答案:

答案 0 :(得分:4)

您可以在类构造函数

中分配如下所示的会话值
class Foo {
    public $Host;
    public function __construct() {
        $this->Host = $_SESSION['SQLIP'];
    }
}

首选使用public关键字代替var来声明变量。

  

注意:出于兼容性原因,仍然支持使用var关键字声明变量的PHP 4方法(作为public关键字的同义词)。在5.1.3之前的PHP 5中,其用法将生成E_STRICT警告。

http://www.php.net/manual/en/language.oop5.visibility.php

答案 1 :(得分:0)

您无法直接为类变量赋值。 将值赋给类构造函数中的变量。

class DB_MSSQL {
    public $Host;

    function __construct(){
        $this->Host = $_SESSION['SQLIP'];
    }
}

找到我的以下编辑的ans以解决您的问题

class DB_MSSQL {
  public $Host;
  public $Database;
  public $User;
  public $Password;

  public $Link_ID;
  public $PConnect;

    function _construct(){
        $this->Host = $_SESSION['SQLIP'];
        $this->Database = $_SESSION['SQLDB'];
        $this->User = $_SESSION['SQLUSER'];
        $this->Password = $_SESSION['SQLPASS'];
    }


  function connect() {
    if ( 0 == $this->Link_ID ) {
      if(!$this->PConnect) {
        $this->Link_ID = mssql_connect($this->Host, $this->User, $this->Password);
      } else {
        $this->Link_ID = mssql_pconnect($this->Host, $this->User, $this->Password);
      }
      if (!$this->Link_ID)
        $this->connect_failed("connect ($this->Host, $this->User, \$Password) failed");
      else
        if (!mssql_select_db($this->Database, $this->Link_ID)) {
          $this->connect_failed("cannot use database ".$this->Database);
        }
    }
  }

但对于db变量

,最好使用protected而不是public