0

I'm trying to connect to a database via MySQL but it does not work. I'm getting the following error:

"Connection failed: No connection could be made because the target machine actively refused it. "

Here's the basic code:

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

 // Create connection
 $conn = new mysqli($servername, $username, $password);

 // Check connection
 if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
 }
 echo "Connected successfully";
 ?> 

I can connect to the database from Excel and also via an ODBC connection with the code below but not via MySQL:

  <?php
  $user = 'username';
  $pass = 'password';
  $server = 'server';
  $database = 'database';

  // No changes needed from now on
  $connection_string = "DRIVER={SQL Server};SERVER=$server;      DATABASE=$database";
  $conn = odbc_connect($connection_string,$user,$pass);

if (!$conn){
   exit("Connection to the database Failed: " . $conn);
}
echo "Connected successfully";
  ?>

Any ideas?

3
  • You are trying to connect using SQL Server driver on one, and mysqli on the other? Is the server running MySQL or Microsoft SQL Server? Commented Sep 29, 2015 at 20:00
  • I'm waiting on the Database admin to confirm but its supposed to be both. Is there a way to check without waiting for the admin? Commented Sep 29, 2015 at 20:11
  • If you're getting a connection refused, it might be running on non-standard port numbers, be firewalled or not exist. Best to get confirmation from your DBA, but I'd be very surprised if the same database is available in both MySQL and MSSQL. Commented Sep 29, 2015 at 20:17

2 Answers 2

1

You say you can connect with odbc which means you have an SQL Server database, not a Mysql database. If you don't like odbc, pdo is your other option. Mysqli is not an option as its mysql only.

Sign up to request clarification or add additional context in comments.

3 Comments

I think you're right but i'm still waiting on confirmation. I really wanted to use MySQL because, correct me if i'm wrong, i lose some functionality with ODBC or PDO. I'm building a project thats going to be using a lot of different tables so perhaps i should import into excel and then export into a local MySQL. What do you guys think? Of course i would have to automate this and perhaps run it nightly..
i think you're confused. mysql and sql server are databases. there is no such thing as a hybrid mysql/sqlserver database. you either have one or the other. the fact that you're connecting to an sql server database means you do not have mysql. that's all the confirmation you need. you're going to have the db admin lol'ing at you when he gets your question. ODBC and PDO are not databases, they are simply drivers that allow you to connect to the database and you will not lose any functionality with either of them. Stick to ODBC and all will be groovy.
well, to clarify, you could have both, but they would not be a hybrid type of thing, you'll still have to use one or the other.
0

Try this code:

<?php
$link = mysqli_connect('server','username','password','database');

if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

echo("success");

?>

3 Comments

I tried you code and got the following message: Error: Unable to connect to MySQL. Debugging errno: 2002 Debugging error: No connection could be made because the target machine actively refused it.
Its either because the host computer does not have a MySQL server running or because firewall is blocking port. Are you running a LAMP or WAMP stack?
I'm running a WAMP stack.

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.