2012-03-14 79 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";