I have another PHP question for you. I have a class that is supposed to go into a database and find labels which will be used in the main program file. When I try to connect to my database, it seems to not like the fact that I’m using a function to set the value of a variable (I am guessing that’s why I has the “unexpected (” error)
Here is the class:
class lblFinder
{
//declarations
private $dbConnection=mysql_connect("localhost", "root", "");
private $dbName="matecalculator";
private $cmd="";
private $size=0;
//end of declarations
public function setSize($shape)
{
if($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($this->cmd,$dbConnection);
//get results
while (($Row = mysql_fetch_row($QueryResult)) !== 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($QueryResult)) !== FALSE)
{
$l[i]=$Row[0];
$i++;
}
mysql_free_result($qryResults);
return l;
}
}
I use a test file to pass dummy values to the application. The relevant code is as follows:
$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";
}
I know that “Round” is a valid value in my DB. I also know that my logic works, because I built this exact app in ASP.NET, and it worked (unfortunately the client didn’t tell us asp wouldn’t work until after it was written…so I need to use PHP)
The functioning ASP code is as follows
public class lblFinder
{
private static string connectionString;
private MySqlConnection con;
private MySqlCommand cmd;
private MySqlDataReader reader;
private int size;
private int i = 0;
public lblFinder(string connStr, MySqlConnection connection, MySqlCommand c, MySqlDataReader r)
{
connectionString = connStr;
con = connection;
reader = r;
cmd = c;
cmd.Connection = con;
}
public void setSize(string shapeName)
{
cmd.CommandText = "SELECT COUNT(varID) FROM tblVariables WHERE shapeID IN(SELECT shapeID FROM tblShapes WHERE shapeName= '" + shapeName + "')";
reader = cmd.ExecuteReader();
if (reader.Read())
{
size = reader.GetInt32(0);
}
reader.Close();
}
public int getSize()
{
return size;
}
public string[] setLabels(string[] l, string shapeName)
{
i = 0;
cmd.CommandText = "SELECT varDesc FROM tblVariables WHERE shapeID IN(SELECT shapeID FROM tblShapes WHERE shapeName= '" + shapeName + "')";
reader = cmd.ExecuteReader();
while (reader.Read())
{
l[i] = reader.GetString("varDesc");
i++;
}
reader.Close();
return l;
}
}
In your variable declaration, you cannot call a function.
private $dbConnection=mysql_connect("localhost", "root", "");Instead, use a constructor: