Error code on page:
SQL Error: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
Warning: Undefined variable $mysql in .../phplogin/register.php on line 11
Fatal error: Uncaught Error: Call to a member function prepare() on null in .../www/phplogin/register.php:11 Stack trace: #0 {main} thrown in .../phplogin/register.php on line 11
Code in register.php [variables like pw, pw2 and username are definded in the html part of the file, which i cant insert here]:
<?php
if(isset($_POST['submit'])) {require("mysql.php");
$stmt = $mysql->prepare("SELECT * FROM accounts WHERE USERNAME = :user");// Check username
$stmt->bindParam(":user", $_POST['username']);
$stmt->execute();
$count = $stmt->rowCount();
if($count == 0) {
// Username not used
if($_POST['pw'] == $_POST['pw2']) {
//User anlegen
$stmt = $mysql->prepare("INSERT INTO accounts (USERNAME, PASSWORD) VALUES (:user, :pw");
$stmt->bindParam(":user", $_POST['username']);
$hash = password_hash($_POST['pw'], PASSWORD_BCRYPT);
$stmt->bindParam(":pw", $hash);
$stmt->execute();
echo "Dein Account wurde angelegt"; // Added account into database
} else {
echo "Passwörter stimmen nicht überein"; // pw and pw2 are not the same
}
} else {
echo "Username bereits vergeben"; // Username used already
}
}
?>
Code in mysql.php:
<?php
$host = '[the host]';
$name = '[the database name]';
$user = '[the user]';
$password = '[the user password]';
try {
$mysql = new PDO("mysql:host=§host;dbname=$name", $user, $password);
} catch (PDOException $e) {
echo "SQL Error: ".$e->getMessage();
}
?>
I use the hoster bplaced on freestyle plan so i cant just restart the database servers.
I wrote this to create the tables: CREATE TABLE accounts (USERNAME varchar(255) UNIQUE, PASSWORD varchar(255));