2012-03-13 54 views
0

我有另一個PHP問題給你,我有一個類應該進入數據庫並找到將在主程序文件中使用的標籤。連接到我的數據庫,它似乎不喜歡我使用函數來設置變量的值(我猜這就是爲什麼我有「意外(」錯誤)的事實意外的是(當使用函數設置變量的值時

這裏是類:

class lblFinder 
     { 
      //declarations 
       private $dbConnection=mysql_connect("localhost", "root", ""); 
       private $dbName="matecalculator"; 

       private $cmd=""; 
       private $size=0; 
      //end of declarations 

      public function setSize($shape) 
      { 
       if($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($this->cmd,$dbConnection); 

         //get results 
         while (($Row = mysql_fetch_row($QueryResult)) !== 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($QueryResult)) !== FALSE) 
       { 
        $l[i]=$Row[0]; 
        $i++; 
       } 
       mysql_free_result($qryResults); 
       return l; 
      } 
     } 

我使用測試文件來虛擬值傳遞給應用程序相關的代碼如下:。

 $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"; 
     } 

我知道「Round」是我DB中的有效值。我也知道我的邏輯工作,因爲我在ASP.NET中構建了這個確切的應用程序,它的工作(不幸的是,客戶端並沒有告訴我們,asp寫入之前不會工作...所以我需要使用PHP )

運作的ASP代碼如下

public class lblFinder 
{ 
private static string connectionString; 
private MySqlConnection con; 
private MySqlCommand cmd; 
private MySqlDataReader reader; 
private int size; 
private int i = 0; 

public lblFinder(string connStr, MySqlConnection connection, MySqlCommand c, MySqlDataReader r) 
{ 
    connectionString = connStr; 
    con = connection; 
    reader = r; 
    cmd = c; 
    cmd.Connection = con; 
} 

public void setSize(string shapeName) 
{ 
    cmd.CommandText = "SELECT COUNT(varID) FROM tblVariables WHERE shapeID IN(SELECT shapeID FROM tblShapes WHERE shapeName= '" + shapeName + "')"; 
    reader = cmd.ExecuteReader(); 
    if (reader.Read()) 
    { 
     size = reader.GetInt32(0); 
    } 
    reader.Close(); 
} 

public int getSize() 
{ 
    return size; 
} 

public string[] setLabels(string[] l, string shapeName) 
{ 
    i = 0; 

    cmd.CommandText = "SELECT varDesc FROM tblVariables WHERE shapeID IN(SELECT shapeID FROM tblShapes WHERE shapeName= '" + shapeName + "')"; 
    reader = cmd.ExecuteReader(); 

    while (reader.Read()) 
    { 
     l[i] = reader.GetString("varDesc"); 
     i++; 
    } 
    reader.Close(); 

    return l; 
} 
} 
+2

太多的代碼。我們不需要爲PHP錯誤查看.NET代碼。它在哪個行號報告錯誤? – 2012-03-13 19:42:10

+0

那麼錯誤發生在哪裏?什麼線? – paulsm4 2012-03-13 19:42:53

+1

你能發佈實際的錯誤信息嗎?它應該告訴你一個行號,看看(和周圍)該行。你可能錯過了'}',''''或';'。 – 2012-03-13 19:44:03

回答

4

在你的變量聲明,你不能調用一個函數。

private $dbConnection=mysql_connect("localhost", "root", "");

相反,使用構造函數:

private $dbConnection; 
function __construct() { 
    $this->dbConnection=mysql_connect("localhost", "root", ""); 
} 
+0

認爲這是一個技術性的東西。謝謝! – Cityonhill93 2012-03-13 19:49:47

+0

所以現在它是說$ dbConnection是一個未定義的變量(這導致了其他一些錯誤,我懷疑如果我們找到這個錯誤,它應該修復其他錯誤) – Cityonhill93 2012-03-13 23:44:35