我还有另一个PHP问题。我有一个应该进入数据库的类,并找到将在主程序文件中使用的标签。当我尝试连接到我的数据库时,似乎不喜欢我正在使用函数来设置变量的值(我猜这就是为什么我有“意外(”错误)
这是班级:
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;
}
}
我使用测试文件将虚拟值传递给应用程序。相关代码如下:
$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";
}
我知道“Round”是我的数据库中的有效值。我也知道我的逻辑工作,因为我在ASP.NET中构建了这个确切的应用程序,并且它工作(不幸的是客户端没有告诉我们asp直到它被编写后才会工作...所以我需要使用PHP )
正常运行的ASP代码如下
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;
}
}
答案 0 :(得分:4)
在变量声明中,您无法调用函数。
private $dbConnection=mysql_connect("localhost", "root", "");
相反,使用构造函数:
private $dbConnection;
function __construct() {
$this->dbConnection=mysql_connect("localhost", "root", "");
}