0

I have a MySQL class that I use to connect to MySQL. But after doing the upgrade of PHP to 5.4.16, it doesn't work any more. Can anyone help with it?
Below is the code that I use. Whenever I try to connect it gets into the error part.

final class MySQL {
    private $connection;

    public function __construct($hostname, $username, $password, $database) {
            if (!$this->connection = mysql_connect($hostname, $username, $password)) {
            exit('Error: Could not make a database connection using ' . $username . '@' . $hostname);
    }
}
Norbert
  • 6,026
  • 3
  • 17
  • 40
Gops
  • 129
  • 1
  • 5
  • 2
    You're going to get more useful information if you look in `mysql_error`. But you should really look at upgrading to `mysqli_` or `PDO`, as the `mysql_` library is deprecated, and indeed removed from PHP 7 – andrewsi Sep 08 '16 at 22:13
  • 1
    Please do not use `mysql_*` functions. They are deprecated and removed in the latest version of PHP. Read http://stackoverflow.com/a/12860046/934739. – Gerard Roche Sep 08 '16 at 22:32
  • It looks like you're writing your own ORM. Have you evaluated [Doctrine](http://www.doctrine-project.org/), [Propel](http://propelorm.org/) or [Eloquent](https://laravel.com/docs/5.3/eloquent) before going down this path? – tadman Sep 08 '16 at 22:38
  • I don't think its based on mysql_ deprecated.When I use the mysql_connect outside of class, it works. But when using it inside the above class, it shows error. The error that I receive is "Can't connect to MySQL server on '[mysql server]' (13)" – Gops Sep 08 '16 at 22:40
  • OK. I narrowed it a bit more. When I run the script on linux shell like ">php script.php" it is able to connect. But when executing the same on browser it throws the above error of not able to connect. – Gops Sep 08 '16 at 23:20

1 Answers1

0

SOLVED : Did the following on command prompt and
setsebool -P httpd_can_network_connect=1

More details here : https://maanasroyy.wordpress.com/2015/06/04/php-cant-connect-to-mysql-with-error-13-but-command-line-can/

Gops
  • 129
  • 1
  • 5