-2

I designed a PHP Class with a Static Properties and I get a blank page without any error log, Kindly assist me whats wrong in my PHP Code?

<?php

ini_set("display_startup_errors", 1);
ini_set("display_errors", 1);
error_reporting(-1);

class Response
{
    public $Status;
    public $Message;

    function __construct() {
        $this->Status = FALSE;
        $this->Message = "";
    }        
}

$response = new Response();

class Connection
{
    static private $server = "localhost"
    static private $database = "Student";
    static private $user = "ram";
    static private $password = "ram12345!";

    static public $link;

    public static function Trigger() {
        try
        {
            if (!isset(self::$link)) {
                self::$link = mysql_connect("localhost", "bbminfoc_bbm", "Princess4BBM.>") or die("Couldn't make connection.");
                if(!self::$link)
                {
                    $response->Status = FALSE;
                    $response->Message = "Database Connection Failed !";
                }
                else
                {
                    if(!mysql_select_db(self::$database, self::$link))
                    {
                        $response->Status = FALSE;
                        $response->Message = "Couldn't select database Main !";
                    }
                }
            }
        }
        catch(Exception $e) {
            $response->Status = FALSE;
            $response->Message = "Exception: " . $e->getMessage();
        }

        if(!$response->Status)
        {
            unset(self::$link);
        }
    }
}

if(!isset(Connection::link))
{
    Connection::Trigger();
}

$outp = json_encode($response);

if(isset($outp))
{
    echo($outp);
}
else
{
    echo("No Data");
}

?>

Kindly assist me in this code, I don't know what I did wrong in the above pasted code because I can't able to see the log information in error_log file.

B.Balamanigandan
  • 4,713
  • 11
  • 68
  • 130
  • Try adding semicolon after `Connection::Trigger();` – Yolo Mar 27 '17 at 16:31
  • Are you _really_ certain that you do _not_ get an entry in your http servers error log file? If you have such a definite syntax error in your code that would mean that you have switched of error logging which is something you should _never_ do... – arkascha Mar 27 '17 at 16:32
  • `json_encode($response);` ? Are you trying to json_encode an object ? – Sahil Gulati Mar 27 '17 at 16:32
  • @SahilGulati - just to print the `JSON` version of a `Response Object`. – B.Balamanigandan Mar 27 '17 at 16:37
  • you have syntax errors, two obvious ones. If you're not getting back parse errors, then **it didn't happen.** Unless it never gets there and you're not executing this the way it should be, such as on a host and not as `file:///`. – Funk Forty Niner Mar 27 '17 at 16:39
  • 1. `static private $server = "localhost"` missing a `;`. 2. `Connection::link` missing a `$` ~> `Connection::$link`. 3. see [this](http://stackoverflow.com/questions/43051756/run-time-error-in-php-static-properties#comment73188157_43051756) 4. don't use `mysql_functions`. 5. `mysql_functions()` don't throw exceptions, your `try `block is useless. – Xorifelse Mar 27 '17 at 16:40
  • @Xorifelse and 3. [It never happened...](http://stackoverflow.com/questions/43051756/run-time-error-in-php-static-properties#comment73188083_43051756) – Funk Forty Niner Mar 27 '17 at 16:41
  • Possible duplicate of [PHP Parse/Syntax Errors; and How to solve them?](http://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – Xorifelse Mar 27 '17 at 16:49
  • @Xorifelse - This is not concept based, **the question is purely issue based**. So we can't say this is duplicate. He is telling he can't able to view the `error_log`, then how could he know problem. We people should help... –  Mar 28 '17 at 05:27

4 Answers4

1

As @Yolo already mentioned in the comment, there's a semicolon missing in Connection::Trigger(); and at static private $server = "localhost". As this will cause a parse error the script will fail before executing and you won't see any errors.

To see those errors you will have to find the php.ini configuration file and set display_errors = on. Also you should consider using an IDE with automatic code linting which will show you potential parse errors while typing the code.

As taken from this answer.

The completely fixed code looks like this:

<?php

ini_set("display_startup_errors", 1);
ini_set("display_errors", 1);
error_reporting(-1);

class Response
{
    public $Status;
    public $Message;

    function __construct() {
        $this->Status = FALSE;
        $this->Message = "";
    }        
}

$response = new Response();

class Connection
{
    static private $server = "localhost";
    static private $database = "Student";
    static private $user = "ram";
    static private $password = "ram12345!";

    static public $link = null;

    public static function Trigger() {
        global $response;

        try
        {
            if (self::$link !== null) {
                self::$link = mysql_connect("localhost", "bbminfoc_bbm", "Princess4BBM.>") or die("Couldn't make connection.");
                if(!self::$link)
                {
                    $response->Status = FALSE;
                    $response->Message = "Database Connection Failed !";
                }
                else
                {
                    if(!mysql_select_db(self::$database, self::$link))
                    {
                        $response->Status = FALSE;
                        $response->Message = "Couldn't select database Main !";
                    }
                }
            }
        }
        catch(Exception $e) {
            $response->Status = FALSE;
            $response->Message = "Exception: " . $e->getMessage();
        }

        if(!$response->Status)
        {
            self::$link = null;
        }
    }
}

if(Connection::$link !== null)
{
    Connection::Trigger();
}

$outp = json_encode($response);

if(isset($outp))
{
    echo($outp);
}
else
{
    echo("No Data");
}

?>
Community
  • 1
  • 1
JensV
  • 3,997
  • 2
  • 19
  • 43
  • some errors don't display even with `display_errors = on` because it displays to the stderr. Can you run it from the bash shell? – Forbs Mar 27 '17 at 16:36
  • *"As taken from this answer."* - They're already using that. – Funk Forty Niner Mar 27 '17 at 16:36
  • @B.Balamanigandan updated the answer to include the fixed code – JensV Mar 27 '17 at 16:37
  • @B.Balamanigandan you could also use [this](http://phpcodechecker.com/) site to quickly check the syntax of your code. – JensV Mar 27 '17 at 16:38
  • Thanks for your answer I got an following errors `Notice: Undefined variable: response in /home1/abc/public_html/conn.php on line 54 Notice: Trying to get property of non-object in /home1/abc/public_html/conn.php on line 54 Fatal error: Attempt to unset static property Connection::$link in /home1/abc/public_html/conn.php on line 56` – B.Balamanigandan Mar 27 '17 at 16:44
  • @B.Balamanigandan The `$response` variable isn't set in the function. If you want to use the global variable you declared earlier you will need to use `global $response;` at the start of your function. Plus you can't unset static variables. Set it to `null` instead. If that helps you, upvote and accept the answer. – JensV Mar 27 '17 at 16:47
1

In static private $server = "localhost" you missed the last ;

Also, i'm getting this error:
Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead) in ... on line 61

So, change if(!isset(Connection::link)) to if (Connection::$link !== null)

That should solve your problem.

NOTE
Please do not use mysql_* extension. It's deprecated. If you want to continue using mysql, change to PDO or mysqli

Condorcho
  • 503
  • 4
  • 12
1

U dont need to unset connection it will automatically be unset.. after class,

u missed semicolon after localhost, u didn't call response inside function trigger...

<?php


    ini_set("display_startup_errors", 1);
    ini_set("display_errors", 1);
    error_reporting(-1);

    class Response
    {
        public $Status;
        public $Message;

        function __construct() {
            $this->Status = FALSE;
            $this->Message = "";
        }        
    }

    $response = new Response();

    class Connection
    {
        static private $server = "localhost";
        static private $database = "listings";
        static private $user = "root";
        static private $password = "";

        static public $link;

        public static function Trigger() {

            global $response;
            try
            {
                if (!isset(self::$link)) {
                    self::$link = mysql_connect("localhost", "root", "") or die("Couldn't make connection.");
                    if(!self::$link)
                    {
                        $response->Status = FALSE;
                        $response->Message = "Database Connection Failed !";
                    }
                    else
                    {
                        if(!mysql_select_db(self::$database, self::$link))
                        {
                            $response->Status = FALSE;
                            $response->Message = "Couldn't select database Main !";
                        }
                    }
                }
            }
            catch(Exception $e) {
                $response->Status = FALSE;
                $response->Message = "Exception: " . $e->getMessage();
            }

        }
    }

    if(!isset(Connection::$link))
    {
        Connection::Trigger();
    }

    $outp = json_encode($response);

    if(isset($outp))
    {
        echo "hello";
        echo($outp);
    }
    else
    {

        echo "hello";
        echo("No Data");
    }
Sugumar Venkatesan
  • 4,019
  • 8
  • 46
  • 77
0

Change this to:

if(!isset(Connection::link))
{
    Connection::Trigger();
}

This:

if(!isset(Connection::$link))
{
    Connection::Trigger();
}
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42