注意:变量似乎未初始化

时间:2016-01-21 19:01:42

标签: php

出于某种原因,我在我的代码中收到了此通知。

  

变量$ conn似乎未初始化

我不明白为什么我会看到这个通知。我想我将include包括在正确的位置。

Class Calendar {
  public function show() { 
    include './includes/dbconn.php'; 
    include_once './includes/functions.php'; 

   for( $i=0; $i<$weeksInMonth; $i++ ){
       //Create days in a week
       for($j=1;$j<=7;$j++){

        $cal_date = (string)$this->currentDate;
        $tutor_date = display_tutor_schedule($conn,$cal_date);

        if(isset($tutor_date[$j]['date'])){
           $content.=$this->_showDay($i*7+$j, $tutor_date[$j]['date']);
        }else {
           $content.=$this->_showDay($i*7+$j, 0);
        }
     } 

   $content .="</tr>";  
    }

  }

}

我的$conn变量来自include './includes/dbconn.php';。由于我没有收到任何PHP数据库错误,例如“未连接到数据库”或类似的东西,我认为我的连接是正确的。

的functions.php

function display_tutor_schedule($conn,$tutor_date){

$query = "select * from [dbo].[TUTOR_SCHEDULE] "
        . "LEFT JOIN [dbo].[TUTOR] "
        . "ON [dbo].[TUTOR_SCHEDULE].tutor_id = [dbo].[TUTOR].tutor_id "
        . "LEFT JOIN [dbo].[STATUS] "
        . "ON [dbo].[STATUS].status_id = [dbo].[TUTOR_SCHEDULE].status_id "
        . "WHERE [dbo].[TUTOR_SCHEDULE].date = '$tutor_date' " ;

$stmt = sqlsrv_query($conn,$query);

$i = 0;
$appt_detail = array();
while ($row = sqlsrv_fetch_array($stmt)){
    $appt_detail[$i]['date'] = $row['date'];
    $appt_detail[$i]['t_shedule_id'] = $row['t_shedule_id'];
    $appt_detail[$i]['start_time'] = $row['start_time']; 
    $appt_detail[$i]['end_time'] = $row['end_time'];
    $appt_detail[$i]['tutor_fname'] = $row['tutor_fname'];
    $appt_detail[$i]['tutor_lname'] = $row['tutor_lname'];
    $appt_detail[$i]['status_name'] = $row['status_name'];
    $appt_detail[$i]['status_id'] = $row['status_id'];

    $i++;
}




return $appt_detail;

}

my_class.php

    <?php 

$calendar = new Calendar();
echo $calendar->show();

 ?>

dbconn.php

$serverName = "myserver";
 $connectionInfo = array("Database" => "my_database", "UID" => "user", "PWD" => "pwd");
 $conn = sqlsrv_connect($serverName, $connectionInfo);

3 个答案:

答案 0 :(得分:0)

如果您使用的是netbeans或phpstorm,那么这可能是IDE问题。

检查https://netbeans.org/projects/php/lists/users/archive/2013-03/message/49phpstorm warning php variable might not have been defined

但是,建议您向我们展示您所包含的文件以进行检查。

答案 1 :(得分:-2)

由于变量首先在dbconn.php中初始化,因此IDE可能无法识别它。插入

 $conn = null;
行后

public function show() { 

答案 2 :(得分:-2)

不要使用包含或全局变量,这是不好的。

相反,你应该使用类

class Database {

    private $conn;

    public function __construct(){
        $serverName = "myserver";
        $connectionInfo = array("Database" => "my_database", 
                                "UID" => "user", 
                                "PWD" => "pwd");
        $this->conn = sqlsrv_connect($serverName, $connectionInfo);
    }

    public function  get_connection(){
        return $this->conn;
    }
}

<强> Calendar.php

class Calendar
{
    private $conn;
    public $weeksInMonth;

    function __construct($conn){
        $this->conn = $conn;
    }


    public function show()
    {
        $content = "";
        for ($i = 0; $i < $this->weeksInMonth; $i++) {
            //Create days in a week
            for ($j = 1; $j <= 7; $j++) {
                $cal_date = (string)$this->currentDate;
                $tutor_date = display_tutor_schedule($cal_date);

                if (isset($tutor_date[$j]['date'])) {
                    $content .= $this->_showDay($i * 7 + $j, $tutor_date[$j]['date']);
                } else {
                    $content .= $this->_showDay($i * 7 + $j, 0);
                }
            }
            $content .= "</tr>";
        }
        return $content;
    }

    function display_tutor_schedule($tutor_date)
    {
        $query = "select * from [dbo].[TUTOR_SCHEDULE] "
            . "LEFT JOIN [dbo].[TUTOR] "
            . "ON [dbo].[TUTOR_SCHEDULE].tutor_id = [dbo].[TUTOR].tutor_id "
            . "LEFT JOIN [dbo].[STATUS] "
            . "ON [dbo].[STATUS].status_id = [dbo].[TUTOR_SCHEDULE].status_id "
            . "WHERE [dbo].[TUTOR_SCHEDULE].date = '$tutor_date' ";

        $stmt = sqlsrv_query($this->conn, $query);

        $appt_detail = array();
        while ($row = sqlsrv_fetch_array($stmt)) {
            $appt_detail[] = $row;
        }
        return $appt_detail;
    }

}

<强>用法

$db = new Database();
$conn = $db->get_connection();
$calendar = new Calendar($conn);
$calendar->weeksInMonth = 4;
echo $calendar->show();