Password hashing and database I would like to ask someone if the code i will put here is Secured for SQL injection and my password hashing and checking while user is logging in is secure and done well.

My Database connection :

$hostname='localhost';
$username='root';
$password='';

try {
    $dbh = new PDO("mysql:host=$hostname;dbname=myshop",$username,$password);
    $dbh-&gt;query ('SET NAMES utf8');
    $dbh-&gt;query ('SET CHARACTER_SET utf8_unicode_ci');
    $dbh-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // &lt;== add this line
} catch (PDOException $e) {
      echo $e-&gt;getMessage();
    }


My registration script :

include_once 'config.php';

$data = file_get_contents("php://input");
$data = json_decode($data, true);

try {
    $stmt = $dbh-&gt;prepare("INSERT INTO `accounts` (`account_id`, `firstname`, `lastname`, `username`, `password`, `email`, `city`, `postalcode`, `adress`, `country`, `role`)
      VALUES (NULL, :firstname, :lastname, :username, :password, :email, :city, :postalcode, :adress, :country, 0) ");
    $stmt-&gt;bindParam(':firstname', $data['firstname']);
    $stmt-&gt;bindParam(':lastname', $data['lastname']);
    $stmt-&gt;bindParam(':username', $data['username']);
    $stmt-&gt;bindParam(':password', password_hash($data['password'], PASSWORD_DEFAULT));
    $stmt-&gt;bindParam(':email', $data['email']);
    $stmt-&gt;bindParam(':city', $data['city']);
    $stmt-&gt;bindParam(':postalcode', $data['postalcode']);
    $stmt-&gt;bindParam(':adress', $data['adress']);
    $stmt-&gt;bindParam(':country', $data['country']);
    $stmt-&gt;execute();
  } catch (PDOException $e) {
      if (strpos($e-&gt;getMessage(), "for key 'login'") !== false) {
          echo 'Duplicate login entry';
          exit;
      } if (strpos($e-&gt;getMessage(), "for key 'email'") !== false) {
          echo 'Duplicate e-mail entry';
          exit;
      }
      else {
          throw $e;
      }
  }
  echo 'Success';


My login script :

include_once 'config.php';

$data = file_get_contents("php://input");
$data = json_decode($data, true);

try {
  $stmt = $dbh-&gt;prepare("SELECT * from accounts WHERE
    username=:username");
  $stmt-&gt;bindParam(':username', $data['username']);
  $stmt-&gt;execute();
  } catch (PDOException $e) {
          throw $e;
      }

  $rows = $stmt-&gt;rowCount();
  $result = $stmt-&gt;fetchAll(PDO::FETCH_ASSOC);
  $json = json_encode($result);

  if ($rows === 1){
    if (password_verify($data['password'], $result[0]['password'])) {
      echo $json;
      exit;
    }
    else{
      echo 'Wrong password';
      exit;
    }}
  if ($rows === 0) {
    echo 'No account';
    exit;
  }
  else {
    echo 'Something went wrong';
    exit;
Your code is secure against SQL injection but it's largely inefficient. And this army of bindParams's takes the largest part in it. All you need is just to send an array you already have directly into execute():

$stmt-&gt;execute($data);


Registration

On the other hand, your use of try and catch operator for insert is proper, which is a rare sight. The only objection is error messages that shouldn't be bluntly echoed out. Better to collect them and show nicely along with a form. So I would make the whole registration code as

$error = '';
$data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
try {
    $stmt = $dbh-&gt;prepare("INSERT INTO `accounts` (`account_id`, `firstname`, `lastname`, `username`, `password`, `email`, `city`, `postalcode`, `adress`, `country`, `role`)
      VALUES (NULL, :firstname, :lastname, :username, :password, :email, :city, :postalcode, :adress, :country, 0) ");
    $stmt-&gt;execute($data);
} catch (PDOException $e) {
    if (strpos($e-&gt;getMessage(), "for key 'login'")) {
        $error =  'Duplicate login entry';
    } elseif (strpos($e-&gt;getMessage(), "for key 'email'")) {
        $error = 'Duplicate e-mail entry';
    } else {
        throw $e;
    }
}
if ($error) {
    echo $error;
} else {
    header("Location: ...");
    exit;
}  


Login

The login code could be improved as well. I've got a ready made example, Authenticating a user using PDO and password_verify(), you may check it out for the actual code. Here I will just comment on some parts

} catch (PDOException $e) {
    throw $e;
}


I fancy you just copy-pasted this part from the insert example. But if you think on it, it will become clear that this code will behave exactly the same without try, catch and throw operators! So they should be simple taken out. 

$rows = $stmt-&gt;rowCount();


is essentially useless, as you have the fetch result that can be used instead.

$result = $stmt-&gt;fetchAll(PDO::FETCH_ASSOC);


fetchAll() doesn't seem to be a wise choice, as it adds unnecessary dimension to the resulting array. fetch() should be used instead.