【发布时间】:2012-03-14 00:49:26
【问题描述】:
我有一个用 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;
}
}
只是为了好玩和咯咯笑,这是我的测试文件(它传递了虚拟值)。我知道 round 是来自数据库的有效值,所以我知道这不是问题。
$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";
}
【问题讨论】:
-
请完整提供准确的错误信息。
-
确切的错误是第 37 行 C:\wamp\www\matePrecisionCalc\lblFinder.php 中的 dbConnection mysql_query() 期望参数 2 是资源,在 C:\wamp\www\matePrecisionCalc 中给出 null第 37 行的 \lblFinder.php mysql_fetch_row() 期望参数 1 是资源,第 40 行的 C:\wamp\www\matePrecisionCalc\lblFinder.php 中给出的 null在一个循环中。