Jump to content

Database access with php doesnt work


Lasslos05
 Share

Go to solution Solved by Lasslos05,

Recommended Posts

Hey Guys,

I'm building my own website with flutter. It needs a database, so I set up one on strato.de and tried to set up a php connection. See here: db.lh0.eu/db_connection.php The code: 

<?php
$db_server = 'rdbms.strato.de';
$db_benutzer = 'DBU******';
$db_passwort = '******************';
$db_name = 'DBS********'; # Verbindungsaufbau
if(mysql_connect($db_server, $db_benutzer, $db_passwort)) {
echo 'Server-Verbindung erfolgreich, wähle Datenbank aus...';
if(mysql_select_db($db_name)) {
echo 'Datenbank erfolgreich ausgewält, alle Tests abgeschlossen.';
}
else {
echo 'Die angegebene Datenbank konnte nicht ausgewählt werden, bitte die Eingabe prüfen!'; }
}
else {
echo 'Verbindung nicht möglich, bitte Daten prüfen! ';
echo 'MYSQL-Fehler: '.mysql_error();
}
?>

Anything I did wrong? The documentation how to use PHP to access the database is here: https://www.strato-hosting.co.uk/faq/hosting/this-is-how-you-can-use-our-mysql-database/

Link to comment
Share on other sites

Thanks for the reply. I changed the code: 

<?php
$host = 'rdbms.strato.de';
$db = 'DBS*********';
$user = 'DBU******';
$password = '******************';

$dsn = "mysql:host=$host;dbname=$db;charset=UTF8";

try {
   $db = new PDO($dsn, $user, $password);
   echo 'connected';
} catch (PDOException $e) {
   echo $e->getMessage();
}
?>

It still doesn't work. Is there something else wrong? I'm pretty sure the login data is correct. Error Message:

SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'rdbms.webmailer.de' (113)

 

Edited by Lasslos05
Link to comment
Share on other sites

I don't think so. The tutorial from my hosting service (strato.de) uses mysql_connect($db_server, $db_user, $db_password). If there is a port added automatically when using mysql_, then that could be the issue.

 

The code from the offical tutorial from strato:

# Access data
$db_server = 'rdbms.strato.de';
$db_user = '';
$db_passwort = '';
$db_name = ''; # Connection establishment
if(mysql_connect($db_server, $db_user, $db_password)) {
echo 'Server connection successful, select database...
';
if(mysql_select_db($db_name)) {
echo 'Server connection successful, select database ...
}
else {
echo 'The specified database could not be selected, please check the information you have entered!'; }
}
else {
echo 'Connection not possible, please check data! ';
echo 'MYSQL-error: '.mysql_error();
}
?>

 

Edited by Lasslos05
Link to comment
Share on other sites

Did you try and access it via ssh using the instructions they provided?  There is also a mention of a security administration package that could be interfering.  

Just to be clear, the default mysql port is 3306, and since they provide a hosted/shared mysql server, there is no problem with the default port.  

According to some people, there was an odd bug in regards to the DSN string and remote databases.  I have never encountered this myself but perhaps you have an old(er) php stack in your hosting environment?

Try this code, cribbed directly from the Best PDO Tutorial

$host = 'rdbms.strato.de';
$db   = 'DBS*********';
$user = 'DBS*********';
$pass = '***';
$charset = 'utf8mb4';

$dsn = "mysql: host=$host;dbname=$db;charset=$charset";
$options = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];
try {
     $pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
     throw new \PDOException($e->getMessage(), (int)$e->getCode());
}

The space between the scheme 'mysql: ' and host= is deliberate in this case.  Between that and checking out if you can ssh & the security settings you can change, any other issue probably will require some support from your ISP.

Link to comment
Share on other sites

Hey Guys, 

first of all, thank you for the reply's. I taught I would be subscribed to that topic automatically. Apparently, I wasn't. I ran the code of @gizmola exactly how he wrote it except for the error thrown in the catch block, and replaced it with 

     echo $e->getMessage();

I have a good message and a bad message:

Good message: It shows something different!
Bad message: It's an error.

The error is: 

SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

And if I try it with SSH, as @kicken suggested, it throws:

ERROR 2003: (HY000): Can't connect to MySQL server on 'rdbms.webmailer.de' (113)

I've tried it twice, and I'm pretty sure there was no typo. I could not find any security settings that could block access. 

Edited by Lasslos05
Link to comment
Share on other sites

  • Solution

I don't know how, but I solved it. I just tried a random script from the internet that looked a little bit different. It's that one:

<?php
    $user = 'DBu*******';
    $pass = '****************';
    $host = 'rdbms.strato.de';
    $db   = 'DBs**********';
    
$conn = new mysqli($host, $user, $pass, $dbdb);
	if ($conn->connect_error) {
    	echo("Connection failed: " . $conn->connect_error);
	} else {
		echo "Success";
	}

?>

Thanks so much to all of you who tried to help me.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.