2012-03-14 80 views
0

我有一個用php編寫的類,應該找到將放在主文件(當我創建它時)的文本框旁邊的標籤。基本上,班級搜索我的數據庫以查找選定形狀的標籤(將從主頁面傳遞)。當我使用虛擬值運行函數時,就好像我的$ dbConnection值沒有設置值。我得到一個錯誤,說$ dbConnection是未定義的,然後更多的錯誤說函數期望某種參數類型,但給出的類型爲空。當我看,他們都指向$ dbConnection變量。我的班級看起來像這樣:變量沒有設置它的值

class lblFinder 
     { 
      //declarations 
       private $dbConnection; 
       private $dbName="matecalculator"; 

       private $cmd=""; 
       private $size=0; 
      //end of declarations 
      public function __construct() 
      { 
       $this->dbConnection=mysql_connect("localhost", "root", ""); 
      } 

      public function setSize($shape) 
      { 
       if($this->dbConnection===false) 
       { 
        echo "<p>Something went wrong.</p><p> Error Code:".mysql_errno().": ".mysql_error()."</p>"; 
       } 
       else 
       { 
        if(mysql_select_db($this->dbName,$this->dbConnection)) 
        { 
         $cmd="SELECT COUNT(varID) FROM tblvariables WHERE shapeID IN(SELECT shapeID FROM tblshapes WHERE shapeName='$shape')"; 
         $qryResults=mysql_query($cmd,$dbConnection); 

         //get results 
         while (($Row = mysql_fetch_row($qryResults)) !== FALSE) 
         { 
          $size=$Row[0]; 
         } 

         mysql_free_result($qryResults); 
        } 
        else 
        { 
         echo "<p>Something went wrong.</p><p> Error Code:".mysql_errno().": ".mysql_error()."</p>"; 
        } 
       } 
      } 

      public function getSize() 
      { 
       return $this->size; 
      } 

      public function setLabels($shape) 
      { 
       //declarations 
        $l=array(); 
       //end of declarations 

       $this->cmd="SELECT varDesc FROM tblVariables WHERE shapeID IN(SELECT shapeID FROM tblShapes WHERE shapeName= '".$shape."')"; 
       $qryResults=mysql_query($cmd,$dbConnection); 

       $i=0; 
       if(($Row = mysql_fetch_row($qryResults)) !== FALSE) 
       { 
        $l[i]=$Row[0]; 
        $i++; 
       } 
       mysql_free_result($qryResults); 
       return $l; 
      } 
     } 

只是踢和咯咯,這裏是我的測試文件(它通過虛擬值)。我知道這輪是來自DB的有效值,所以我知道這不是問題。

$arr=array(); 
     $lf=new lblFinder; 
     $lf->setSize("Round"); 
     echo "Size=".$lf->getSize(); 
     $arr=$lf->setLabels("Round"); 
     $i=0; 
     foreach($arr AS $label) 
     { 
      echo "Label $i is $label"; 
     } 
+0

請提供完整的確切的錯誤信息。 – deceze 2012-03-14 00:59:51

+0

確切的錯誤是dbConnection在C:\ wamp \ www \ matePrecisionCalc \ lblFinder.php在第37行mysql_query()期望參數2是資源,null在C:\ wamp \ www \ matePrecisionCalc \ lblFinder.php中給出的行37 mysql_fetch_row()期望參數1是資源,在第40行的C:\ wamp \ www \ matePrecisionCalc \ lblFinder.php中給出null(這是一個重複一遍又一遍,就像它在循環中一樣) – Cityonhill93 2012-03-14 03:04:43

回答

1

有一個錯字/錯誤在你的類,在這裏糾正你:

public function setLabels($shape) 
     { 
      //declarations 
       $l=array(); 
      //end of declarations 

      $this->cmd="SELECT varDesc FROM tblVariables WHERE shapeID IN(SELECT shapeID FROM tblShapes WHERE shapeName= '".$shape."')"; 
      $qryResults=mysql_query($cmd,$dbConnection); 

      $i=0; 
      if(($Row = mysql_fetch_row($qryResults)) !== FALSE) 
      { 
       $l[$i]=$Row[0]; // The $ symbol was missing from the i 
       $i++; 
      } 
      mysql_free_result($qryResults); 
      return $l; 
     } 

此外,爲了方便起見,你可以寫這個如下:

public function setLabels($shape) 
     { 
      //declarations 
       $l=array(); 
      //end of declarations 

      $this->cmd="SELECT varDesc FROM tblVariables WHERE shapeID IN(SELECT shapeID FROM tblShapes WHERE shapeName= '".$shape."')"; 
      $qryResults=mysql_query($cmd,$dbConnection); 

      if(($Row = mysql_fetch_row($qryResults)) !== FALSE) 
      { 
       $l[]=$Row[0]; // Do not need to increment index of array, php does automatically 
      } 
      mysql_free_result($qryResults); 
      return $l; 
     } 

如果連接返回null,則可以通過輸出連接期間發生的錯誤來解決該問題,如下所示:

$this->dbConnection=mysql_connect("localhost", "root", "") or die("Error connecting to DB: " . mysql_error()); 

編輯
die() PHP腳本的暫停執行。在這種情況下,它對於調試非常有用,因爲它可以防止腳本的其他部分運行並傾倒一堆錯誤(就像它目前爲您所做的那樣)。 mysql_connect("localhost", "root", "") or die(mysql_error());告訴腳本如果連接不成功,要停止php腳本並輸出mysql錯誤,所以你有信息要排除故障。

其中一個挑戰是,當您告訴我們行號時,您在上面引用的代碼中是哪行?通過行號,PHP可以準確地告訴你失敗的原因。你能告訴我們哪些行是37和40嗎?

+0

仍然給予我的錯誤。確切的錯誤是 dbConnection在C:\ wamp \ www \ matePrecisionCalc \ lblFinder.php在第37行 mysql_query()期望參數2是資源,null在C:\ wamp \ www \ matePrecisionCalc \ lblFinder.php中給出on第37行 mysql_fetch_row()期望參數1是資源,在第40行的C:\ wamp \ www \ matePrecisionCalc \ lblFinder.php中給出的null(這是一遍又一遍地重複,就好像它在循環中一樣 – Cityonhill93 2012-03-14 02:09:40

+0

出於好奇,die()函數做了什麼? – Cityonhill93 2012-03-14 05:39:39

+0

請在我的答案中查看編輯。 – 2012-03-14 14:20:05

0

setSize功能和setLabels功能,您引用$cmd$dbConnection時,你應該使用$this->cmd$this->dbConnection

在這兩個函數中都沒有定義局部變量$dbConnection

此外,正如@cale_b指出的那樣,您的循環中有一個i的拼寫錯誤,您可以使用or die()來停止執行腳本的其餘部分。根據這個類的用法,最好返回一個錯誤,而不是停止所有的PHP執行。如果你想這樣做,你可以這樣做:

$this->dbConnection = mysql_connect("localhost", "root", ""); 
if (!$this->dbConnection) 
    return "error";