2013-05-06 55 views
0
<? 
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都是空的?php class _Construct empty

如果我测试$ _SESSIONS DB_MSSQL之间都可以。 类没有错误,但这个变量是空的...我不知道为什么.. 任何人都可以帮助我吗? 非常感谢你!

+1

'_construct'(单引号下划线)是一个错字或一个非常差的名称选择。 – 2013-05-06 14:32:08

回答

3

您已经错过了一个底线,你应该写:

public function __construct() 

它应该是这样的。

+0

是工作!谢谢 – 2013-05-06 15:01:33