0

For some reason I am getting this notice in my code.

Variable $conn seems to be uninitialized

I don't understand why I'm seeing this notice. I think I'm including my include in the right place.

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

}

My $conn variable is coming from include './includes/dbconn.php';. Since I am not getting any PHP database error, such as "Not connected to the database" or something like that, I assume that my connection is right.

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);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Maduro
  • 713
  • 5
  • 23
  • 44
  • Are you using PHPStorm? – Mahmoud Tantawy Jan 21 '16 at 19:05
  • 2
    We have no idea what `/includes/functions.php` contains/does so there is no sane way to guess a solution. – PeeHaa Jan 21 '16 at 19:05
  • @MahmoudTantawy nop, netbeans – Maduro Jan 21 '16 at 19:05
  • @PeeHaa the issue is in `/includes/dbconn.php` not in `/includes/functions.php` – Maduro Jan 21 '16 at 19:07
  • Looks like an issue with netbeans and phpstorm, check https://netbeans.org/projects/php/lists/users/archive/2013-03/message/49 and http://stackoverflow.com/questions/11751330/phpstorm-warning-php-variable-might-not-have-been-defined – Mahmoud Tantawy Jan 21 '16 at 19:07
  • @ArielMaduro well considering you haven't shared either it's hard to guess either way – PeeHaa Jan 21 '16 at 19:08
  • Where does said error come from? Does PHP tell you there is a problem or your IDE? Did you run the code? – PeeHaa Jan 21 '16 at 19:09
  • @PeeHaa I use the same connection with all my projects, the only different in this project is that I am doing a `class` I've never use my connections or `include` anything in a class...So, I thought this issue is more about my class than my files – Maduro Jan 21 '16 at 19:10
  • so, where are you initializing that class? the error it's throwing says it all. *Variable $conn seems to be **uninitialized*** – Funk Forty Niner Jan 21 '16 at 19:11
  • The error PHP gives you for an undefined variable includes all of the information needed to debug the problem. For example, it will give you the name of the variable that was not defined (which you have included here `$conn`), as well as the exact line number on which the undefined variable triggered the error (which have not included here), and the exact file in which the error was triggered (which have also not included here). – Sherif Jan 21 '16 at 19:11
  • @ArielMaduro we dont care about `my_class.php` we wanna see `dbConn.php` – meda Jan 21 '16 at 19:13
  • @Sherif the line is inluded there `$tutor_date = display_tutor_schedule($conn,$cal_date);` – Maduro Jan 21 '16 at 19:13
  • @ArielMaduro what about `$weeksInMonth` and `$content` where are there defined – meda Jan 21 '16 at 19:21
  • @Fred-ii- Since I thought the error was in the class, I just try to write a small example here so people could provide me some answers. Sometimes when people see a lot of code they don't like to read questions..so I just tried to make my code as simple as possible – Maduro Jan 21 '16 at 19:22

3 Answers3

0

If you are using NetBeans or PhpStorm, then this might be IDE issue.

Check https://netbeans.org/projects/php/lists/users/archive/2013-03/message/49 and PhpStorm warning PHP variable might not have been defined

However, it is advisable that you show us the files you include to check them.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mahmoud Tantawy
  • 713
  • 5
  • 12
-1

Don't use includes or global for your variables. It's bad.

Instead you should be using classes:

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

}

Usage

$db = new Database();
$conn = $db->get_connection();
$calendar = new Calendar($conn);
$calendar->weeksInMonth = 4;
echo $calendar->show();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
meda
  • 45,103
  • 14
  • 92
  • 122
-2

Since the variable is first initialized in the dbconn.php, the IDE might not recognize it. Insert

 $conn = null;

after the line

public function show() { 
Roxxorfreak
  • 380
  • 2
  • 10
  • The file is included in the scope of where it is called. – PeeHaa Jan 21 '16 at 19:22
  • @PeeHaa exactly, because it is my scope I though I wouldn't have any problem =/ – Maduro Jan 21 '16 at 19:23
  • @ArielMaduro what gives you the error? Is it when you run the php code or is it an IDE warning? – PeeHaa Jan 21 '16 at 19:24
  • Have you.... like... idunno.... actually ran the code to check whether it is just your IDE getting confused instead of an actual error in your code? Because it sounds like @MahmoudTantawy is pretty much in the money. – PeeHaa Jan 21 '16 at 19:26
  • The IDE might not check if the variable is defined in the included file. So if you don't get a warning when executing the code, there is no problem. – Roxxorfreak Jan 21 '16 at 19:27
  • @ArielMaduro You may do the following: Include $conn = null; before including the dbconn.php. – Roxxorfreak Jan 21 '16 at 19:30