I have a class written in php that is supposed to find the labels that will be put beside textboxes in the main file (when I create it). Basically the class searches through my DB to find the labels for the selected shape(which will be passed from the main page). When I run the functions using dummy values, it is as if my $dbConnection value is not setting the value. I get an error saying that $dbConnection is undefined, and then more errors saying that functions expect a certain parameter type, but the type given is null. When I look, they are all pointing to the $dbConnection variable. My class looks like this:
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;
}
}
Just for kicks and giggles, here is my test file (which passes dummy values). I know that round is a valid value from the DB, so I know that is not the problem.
$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";
}
There is a typo/bug in your class, corrected here for you:
Also, for simplicity sake, you could have written this as follows:
If the connection is returning null, then you can troubleshoot that issue by outputting the error that is happening during connection as follows:
EDIT:
die()halts execution of the PHP script. In cases like this, it’s useful for debugging, since it prevents the rest of the script running and dumping a bunch of errors (like it is doing for you currently).mysql_connect("localhost", "root", "") or die(mysql_error());tells the script that if the connection is not successful, to halt the php script and output the mysql error, so you have information to troubleshoot with.One of the challenges is that while you are telling us the line number, but which line is that in your code quoted above? With the line number, PHP is telling you exactly where the failure is. Can you tell us which line(s) are 37 and 40?