1

I have newly purchased server, on that server database connections are not working.

<?php
    error_reporting(E_ALL);
    $server   = "server name";
    $user     = "username";
    $password = "password";
    $db       = "test";
    echo "Before";
    $con = mysql_connect($server, $user, $password);
    echo "After";
    if (!$con){
        die('Could not connect:' . mysql_error());
    }
    mysql_select_db($db, $con);
?>

When run this file its print Before text but not print After text.

halfer
  • 19,824
  • 17
  • 99
  • 186
sridhard
  • 119
  • 3
  • 15

1 Answers1

1

Currently you can use the following code:

ini_set("error_reporting", E_ALL & ~E_DEPRECATED); 

Using this you can get either deprecated or not.

FYI: The mysql_* functions have been removed in PHP7.x. There are two modules you can use.

The first is MySQLi, just use the code as follows:

<?php
$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();
  }
?>

You can also use PDO using code :

<?php
$servername = "localhost";
$username = "username";
$password = "password";

try {
    $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }
?>
halfer
  • 19,824
  • 17
  • 99
  • 186
Deep Kakkar
  • 5,831
  • 4
  • 39
  • 75
  • Formatting tip: when writing names of software items such as "MySQL" or "PHP 7", it is better not to put them in `inline formatting`. The names of things are not themselves code or console I/O (there are exceptions such as talking about the `php` executable, but that doesn't apply here). – halfer Mar 31 '17 at 09:10