0

I have been trying to spot the problem in the

<?php
require_once("config.php");

class MYSQLDatabase{

    private $connection;
// open the connection as soon as object is created
function __construct(){

    $this->open_connection();
}

public function open_connection(){

    $this->connection = mysql_connect("DB_SERVER", "DB_USER", "DB_PASS");

    if(!$this->connection){
        die("Database failed " . mysql_error());
    }else{
     $db_select = mysql_select_db(DB_NAME, $this->connection);   
        if(!$db_select){
            die("Database connection failed " . mysql_error());
        }
    }
}

    public function close_connection(){
        if(isset($this->connection)){
            mysql_close($this->connection);
            unset($this->connection);
        }
    }

    public function query($sql){
        $result = mysql_query($sql, $this->connection);
        $this->confirm_query($result);
        return $result;
    }



    private function confirm_query($result){
        if(!result){
            die("Database query failed: " . mysql_error());
        }
    }
}
$database = new MYSQLDatabase();

?>

When I go to the index.php file and test the class with the code below i get the following error:
This page isn’t working

localhost is currently unable to handle this request. HTTP ERROR 500.

require_once("../includes/database.php");
if(isset($database)){
    echo "true";
}else{
    echo "false";
}
lotfio
  • 1,916
  • 2
  • 18
  • 34
  • Your logs should show the cause of that error 500 – Mark Baker Nov 19 '17 at 19:17
  • turn on error reporting from php ini file or add this `error_reporting(E_ALL); ini_set("display_errors", 1);` to the top of your config file and show us the errors that you are getting – lotfio Nov 19 '17 at 19:18
  • Please start using `mysqli_*` or `PDO` as `mysql_*` is deprecated and removed as of PHP 7 – SuperDJ Nov 19 '17 at 19:18
  • So where ever i said mysql_ something i should say mysqli_something right? – Siyad Nijah Nov 19 '17 at 19:20
  • @SiyadNijah please delete this comment and append it to your post, so your problem is that you are using new php version in which `mysql` is removed so please use `mysqli` instead or `PDO` – lotfio Nov 19 '17 at 19:26
  • Possible duplicate of [The following code returns an 500 error as the code is deprecited in php version 7, How to make it work in php verison 7?](https://stackoverflow.com/questions/39455416/the-following-code-returns-an-500-error-as-the-code-is-deprecited-in-php-version) – mickmackusa Nov 19 '17 at 20:03

1 Answers1

0

As I have just mentioned in my comment mysql extension is deleted from PHP 7 as PHP manual is saying here mysql is deleted from php 7 so that's why you are getting and error undefined function mysql

Use mysqli which is mysql improved version. here is an example if you don't know how to use it.

<?php
 // no need fo mysqli_select_db 
$con = mysqli_connect("localhost","my_user","my_password","my_db");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>

For more example check out W3schools

lotfio
  • 1,916
  • 2
  • 18
  • 34
  • If you believe your answer is correct, the better response would be to delete your answer and support my possible duplicate closure. Duplicate questions cause unnecessary site bloat, and answering duplicate questions causes unnecessary page bloat. – mickmackusa Nov 19 '17 at 20:06