php类_Construct为空

时间:2013-05-06 14:29:33

标签: php html sql-server class constructor

<?
session_start();
/*
echo $_SESSION['SQLIP'];
echo "<br>";
echo $_SESSION['SQLDB'];
echo "<br>";
echo $_SESSION['SQLUSER'];
echo "<br>";
echo $_SESSION['SQLPASS'];
echo "<br>";
*/
class DB_MSSQL {
  private $Host;
    private $Database;
    private $User;
    private $Password;
    public $Link_ID  = 0;
  public $Query_ID = 0;
  public $Record   = array();
  public $Row      = 0;
  public $Errno    = 0;
  public $Error    = "";
  public $Halt_On_Error = "yes";
  public $Auto_Free = 1;     
  public $PConnect  = 0;

    public 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);
  }
}
$_php_major_version = substr(phpversion(), 0, 1);
if((4 > $_php_major_version) or !class_exists("DB_Sql"))
    {
    class DB_Sql extends DB_MSSQL
        {
        function DB_Sql($query = "")
            {
            $this->DB_MSSQL($query);
            }
        }
    }
unset($_php_major_version);
?>

我有一个问题,为什么在DB_MSSQL上我的$ Host,$ Datebase,$ User,$ Password都是空的?

如果我在DB_MSSQL之间测试$ _SESSIONS就行了。 类没有错误,但这个变量是空的......我不知道为什么.. 有谁能够帮我? 非常感谢你!

1 个答案:

答案 0 :(得分:3)

你错过了一个下划线,你应该写:

public function __construct()

它应该像这样工作。