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.

9
  • DO not user mysql_* it is going to be deprecated.. Commented Mar 31, 2017 at 5:31
  • stackoverflow.com/questions/21797118/deprecated-mysql-connect Commented Mar 31, 2017 at 5:31
  • which PHP version are you using ? Commented Mar 31, 2017 at 5:32
  • i am using php 7.1.2 Commented Mar 31, 2017 at 5:33
  • 1
    mysql_* removed from php7, use mysqli or pdo Commented Mar 31, 2017 at 5:34

1 Answer 1

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();
    }
?>
Sign up to request clarification or add additional context in comments.

1 Comment

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.