Pregunta

I am trying to access a database that is not in my localhost or in my network. I am WampServer so I can open the PHP file. I am trying this code bellow, just to see the connection working. but never works.

<!DOCTYPE html><html lang= "en" ><head></head><body>
<?php
        $DBConnect = @mysql_connect("xx.xx.xx.173:22289", "sa", "123456")
            Or die("<p>The database server is not available.</p>"."<p>Error Code" .mysql_connect_erno(). ":". mysql_connect_error());
        echo "<p>Successfully connected to the database server.</p>";

        $DBName = "Database01";
        @mysql_select_db($DBName)
            Or die("<p>The database is not available.</p>" . "<p>Error Code" . mysql_erno($DBConnect). ":" . mysql_error($DBConnect));
        echo "<p>Successfully opened the database.</p>";
mysql_close($DBConnect);
?>
</body>
</html>

This port number they specified for me to use to access their database. I can connect in their virtual machinne trought this ip address, and inside the virtual machine I can access the database using the same ip adress and the the port im using in the code. login and password are, because I can log in in to the database using Microsoft SQL Server.

I tried to change the IP to xx.xx.xx.173,22289 and also did not work. The webpage takes like 7min to load and when it loads, show just the HTML part.

How I can access the database outside through the php file?

If is not possible to access a database especifing an IP adress with WampServer, how should I test the php files?

¿Fue útil?

Solución

The first thing you want change is the mysql* functions, they are depreciated. http://www.php.net/mysql_select_db

You want to use PDO for your database connections in PHP: http://php.net/pdo

try {
    $pdo = new PDO('mysql:host=localhost;dbname=test;', 'user', 'pass');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $pdo->prepare('INSERT INTO DoesNotExist (x) VALUES (?)');
}
catch(Exception $e) {
     echo 'Exception -> ';
     var_dump($e->getMessage());
}

Example error: Exception -> string(91) "SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.doesnotexist' doesn't exist"

Otros consejos

If you want to connect to your server, follow this:

  • before login to whm
  • click SQL Services
  • click Additional MySQL Access Hosts
  • add your ip
  • click to save
  • click again Additional MySQL Access Hosts
  • and click to configure from all users (...configure access from all users' accounts, Click Here)

    $mysql_link = mysql_connect('your_host', 'your_u_name', 'your_pass') or die ('Server bağlantı hatası!');

No need to write the port number. good luck..

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top