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.
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");
}
?>
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
Change this to:
if(!isset(Connection::link))
{
Connection::Trigger();
}
This:
if(!isset(Connection::$link))
{
Connection::Trigger();
}
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");
}