Search code examples
phpservermysql-connect

mysql_connect('mysite.com', 'root', ' '); not working


I have spent my few hours on finding out why mysql_connect('localhost', 'root', ''); works fine on localhost but

mysql_connect('mysite.com', 'root', ''');

does not works on the server ,

can anyone please help !!!


Solution

  • Don't use mysql methods it is deprecated.
    You can use PDO or mysqli
    PDO as:

    try{
       $username = 'root';
       $dbname = 'mysql:host=mysite.com';
       $password = 'password';
       $conn = new PDO($dbname, $username, $password);
       $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
       // do things here
    }
    catch(Exception $e){
       //error
       echo $e->getMessage();
    }