【问题标题】:Variable not setting its value变量未设置其值
【发布时间】:2012-03-14 00:49:26
【问题描述】:

我有一个用 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;
            }
        }

只是为了好玩和咯咯笑,这是我的测试文件(它传递了虚拟值)。我知道 round 是来自数据库的有效值,所以我知道这不是问题。

 $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";
        }

【问题讨论】:

  • 请完整提供准确的错误信息。
  • 确切的错误是第 37 行 C:\wamp\www\matePrecisionCalc\lblFinder.php 中的 dbConnection mysql_query() 期望参数 2 是资源,在 C:\wamp\www\matePrecisionCalc 中给出 null第 37 行的 \lblFinder.php mysql_fetch_row() 期望参数 1 是资源,第 40 行的 C:\wamp\www\matePrecisionCalc\lblFinder.php 中给出的 null在一个循环中。

标签: php variables null


【解决方案1】:

您的课程中有错字/错误,在此为您更正:

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;
        }

如果连接返回 null,那么您可以通过输出连接期间发生的错误来解决该问题,如下所示:

$this->dbConnection=mysql_connect("localhost", "root", "") or die("Error connecting to DB: " . mysql_error());

EDIT:
die() 停止执行 PHP 脚本。在这种情况下,它对调试很有用,因为它可以防止脚本的其余部分运行并转储一堆错误(就像它目前正在为你做的那样)。 mysql_connect("localhost", "root", "") or die(mysql_error()); 告诉脚本如果连接不成功,停止 php 脚本并输出 mysql 错误,以便您有信息进行故障排除。

其中一个挑战是,当您告诉我们行号时,您在上面引用的代码中是哪一行?通过行号,PHP 可以准确地告诉您失败的位置。你能告诉我们哪一行是 37 和 40 吗?

【讨论】:

  • 仍然给我错误。确切的错误是第 37 行 C:\wamp\www\matePrecisionCalc\lblFinder.php 中的 dbConnection mysql_query() 期望参数 2 是资源,C:\wamp\www\matePrecisionCalc\lblFinder.php 第 37 行 mysql_fetch_row 中给出的 null () 期望参数 1 是资源,在第 40 行的 C:\wamp\www\matePrecisionCalc\lblFinder.php 中给出的 null (这是一个反复重复的,就好像它在一个循环中一样。
  • 出于好奇,die() 函数是做什么的?
【解决方案2】:

setSize 函数和setLabels 函数中,当您应该使用$this-&gt;cmd$this-&gt;dbConnection 时,您引用$cmd$dbConnection

在这两个函数中,局部变量 $dbConnection 都没有定义。

另外,正如@cale_b 指出的那样,您的循环中有i 的拼写错误,您可以使用or die() 来阻止脚本的其余部分执行。根据这个类的使用方式,最好返回错误而不是停止所有 PHP 执行。如果你想这样做,你可以这样做:

$this->dbConnection = mysql_connect("localhost", "root", "");
if (!$this->dbConnection)
    return "error";

【讨论】:

    猜你喜欢
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-07
    • 2018-10-18
    • 1970-01-01
    • 1970-01-01
    • 2021-01-01
    相关资源
    最近更新 更多