Php 变量未设置其值
我有一个用php编写的类,它应该可以找到将放在主文件文本框旁边的标签(当我创建它时)。基本上,该类通过MyDB搜索所选形状的标签(将从主页面传递)。当我使用伪值运行函数时,就好像我的$dbConnection值没有设置该值一样。我得到一个错误,说$dbConnection未定义,然后更多的错误,说函数需要某个参数类型,但给定的类型为null。当我查看时,它们都指向$dbConnection变量。我的班级是这样的:Php 变量未设置其值,php,variables,null,Php,Variables,Null,我有一个用php编写的类,它应该可以找到将放在主文件文本框旁边的标签(当我创建它时)。基本上,该类通过MyDB搜索所选形状的标签(将从主页面传递)。当我使用伪值运行函数时,就好像我的$dbConnection值没有设置该值一样。我得到一个错误,说$dbConnection未定义,然后更多的错误,说函数需要某个参数类型,但给定的类型为null。当我查看时,它们都指向$dbConnection变量。我的班级是这样的: class lblFinder { //
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;
}
}
您的类中有一个输入错误/错误,请在此处更正:
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;
}
$this->dbConnection=mysql_connect("localhost", "root", "") or die("Error connecting to DB: " . mysql_error());
如果连接返回null,则可以通过输出连接期间发生的错误来解决该问题,如下所示:
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;
}
$this->dbConnection=mysql_connect("localhost", "root", "") or die("Error connecting to DB: " . mysql_error());
编辑:die()
停止PHP脚本的执行。在这种情况下,它对调试非常有用,因为它可以防止脚本的其余部分运行并转储大量错误(就像它目前为您所做的那样)<代码>mysql\u连接(“本地主机”、“根目录”、“根目录”)或死目录(mysql\u错误())告诉脚本,如果连接不成功,,停止php脚本并输出mysql错误,这样您就有了解决问题的信息
其中一个挑战是,当您告诉我们行号时,上面引用的代码中是哪一行?通过行号,PHP可以准确地告诉您故障所在。您能告诉我们哪一行是37和40吗?在
setSize
函数和setLabels
函数中,当您应该使用$this->cmd
和$this->dbConnection
时,您可以引用$cmd
和$this->dbConnection
在这两个函数中,未定义局部变量$dbConnection
另外,正如@cale_b所指出的,循环中有一个输入错误,i
,您可以使用或die()
来阻止脚本的其余部分执行。根据该类的使用方式,最好返回一个错误,而不是停止所有PHP执行。如果你想这样做,你可以这样做:
$this->dbConnection = mysql_connect("localhost", "root", "");
if (!$this->dbConnection)
return "error";
请完整地提供准确的错误消息。准确的错误是第37行的C:\wamp\www\matePrecisionCalc\lblFinder.php中的dbConnection mysql\u query()预期参数2为资源,第37行的C:\wamp\www\matePrecisionCalc\lblFinder.php中的null预期参数1为资源,第40行的C:\wamp\www\matepricisioncalc\lblFinder.php中给出了null(这是一个像在循环中一样反复出现的错误。仍然给我错误。确切的错误是第37行的C:\wamp\www\matepricisioncalc\lblFinder.php中的dbConnection)第37行的C:\wamp\www\matecrescisioncalc\lblFinder.php中预期参数2为resource,null为空。mysql\u fetch\u row()中预期参数1为resource,在第40行的C:\wamp\www\matecrescisioncalc\lblFinder.php中预期参数1为空(这是一个反复重复的参数,就像在一个循环中一样。出于好奇,die()是什么?)功能是什么?