2012-09-27 42 views
1

我的查詢是:query_id返回任何

$sqlCommand="select `ID` from `freecomputermarket`.`members` where `UserName`='$this->_userName';"; 

我正在usign XAMPP的Apache服務器和我的工作端口(89)

這個類負責數據庫的連接:

<?php 
class MySql 
{ 
    private $_link_Id,$_query_Id,$_serverName,$_userName,$_password,$_dbName,$_rowNum; 

    public function __construct() 
    { 
     $this->_serverName="localhost"; 
     $this->_userName="root"; 
     $this->_password=""; 
     $this->_dbName="freecomputermarket"; 
    } 
    public function connect() 
    { 
     $this->_link_Id=mysql_connect($this->_serverName,$this->_userName,$this->_password); 
     if(!$this->_link_Id) 
     { 
      exit("The Connect is Failed"); 
     } 
     $db_select=mysql_select_db($this->_dbName,$this->_link_Id); 
     if(!$db_select) 
     { 
      exit("Can't Select DataBase"); 
     } 
    } 
    public function query($sqlcommand) 
    { 
     $sqlcommand= addslashes($sqlcommand); 
     //echo $sqlcommand; 
     $this->_query_Id=mysql_query($sqlcommand,$this->_link_Id); 
     exit($this->_query_Id);//print it to check if it is available. 
     if(!$this->_query_Id) 
      exit("Query failed"); 
     $this->_rowNum=mysql_affected_rows(); 
    } 
    public function getRow() 
    { 
     if($this->_rowNum) 
     { 
      return mysql_fetch_assoc($this->_query_Id); 
     } 
    } 
    public function getAllRows() 
    { 
     $arr=array(); 
     $count=0; 
     while($count<$this->_rowNum) 
     { 
      array_push($arr,$this->GetRow()); 
      $count++; 
     } 
     return $arr; 
    } 
    public function getAffectedRowsNumber() 
    { 
     return $this->_rowNum; 
    } 
} 
?> 

這段代碼用於連接到mysql dbms並執行查詢。 打印$ _link_Id時,它有一個值。 打印$ _query_Id時,它什麼都沒有?

+0

請正確縮進你的代碼,其他人需要閱讀它。同時使用'var_dump'進行調試,而不是'exit'。 – hakre

+0

謝謝hakra告訴我關於var_dump的信息 –

+0

如果你正在尋找一些基於mysql_ *'函數的數據庫類,在[這個答案]中有一個(http://stackoverflow.com/a/11580420/367456)包含錯誤處理以及可輕鬆擴展的結果對象。正如下面的評論,'addslashes'不屬於'query'方法。如果你想讓編寫SQL查詢更加舒適,可以添加另一個構建這些字符串的類,比較如下:http://stackoverflow.com/a/12221284/367456(這不完美,但可能會給你一些指示)。 – hakre

回答

0

問題是在這裏

$sqlcommand= addslashes($sqlcommand); 

不要使用和addslashes。

使用這樣

//$sqlcommand= addslashes($sqlcommand); 
$this->_query_Id=mysql_query($sqlcommand,$this->_link_Id); 
+0

相同的問題 –

+0

你可以顯示你的查詢和'$ this - > _ link_Id'值。 –

+0

$ this - > _ link_Id is「Resource id#5」 –

0
<?php 
class MySql 
{ 
    private $_link_Id,$_query_Id,$_serverName,$_userName,$_password,$_dbName,$_rowNum; 

    public function __construct() 
    { 
     $this->_serverName="localhost"; 
     $this->_userName="root"; 
     $this->_password=""; 
     $this->_dbName="freecomputermarket"; 
    } 
    public function connect() 
    { 
     $this->_link_Id=mysql_connect($this->_serverName,$this->_userName,$this->_password); 
     if(!$this->_link_Id) 
     { 
               exit("The Connect is Failed"); 
     } 
     $db_select=mysql_select_db($this->_dbName,$this->_link_Id); 
     if(!$db_select) 
     { 
               exit("Can't Select DataBase"); 
     } 
    } 
    public function query($sqlcommand) 
    { 
             // $sqlcommand= addslashes($sqlcommand); 
              //echo $sqlcommand; 
     $this->_query_Id=mysql_query($sqlcommand,$this->_link_Id); 
              exit($this->_query_Id);//print it to check if it is available. 
     if(!$this->_query_Id) 
               exit("Query failed"); 
     $this->_rowNum=mysql_affected_rows(); 
    } 
    public function getRow() 
    { 
     if($this->_rowNum) 
     { 
               return mysql_fetch_assoc($this->_query_Id); 
     } 
    } 
    public function getAllRows() 
    { 
     $arr=array(); 
     $count=0; 
     while($count<$this->_rowNum) 
     { 
               array_push($arr,$this->GetRow()); 
               $count++; 
     } 
     return $arr; 
    } 
        public function getAffectedRowsNumber() 
        { 
         return $this->_rowNum; 
        } 
} 
?> 

和addslashes()函數的問題

0

的問題是公共函數查詢(SqlCommand的$) 你沒有啓動的範圍內連接。你應該在函數查詢($ sqlcommand)中啓動connect()