我有一個用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