我有一个用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";
}
请提供完整的确切的错误信息。 – deceze 2012-03-14 00:59:51
确切的错误是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