Change the localhost to 127.0.0.1.
And give setAttribute.
Documentation pdo
Create in a separate file the connection.php
Give the include on every page that uses it.
define("PORT", "3306");
define("DB", "login");
define("END", "127.0.0.1");
define("USER", "root");
define("PASS", "123");
function getConexao() {
$conn = new PDO('mysql:host=' . END . ';port=' . PORT . ';dbname=' . DB . ';charset=utf8', USER, PASS);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $conn;
}
Already on call
$conn = getConexao();
$sql = "SELECT * FROM login";
$result = $conn->prepare($sql);
$result->execute();
$usuario = $result->fetch(PDO::FETCH_ASSOC);
**
Comments update.
**
Let's say your call is validated this way.
$retorno = login($_POST['Usuario'], $_POST['Senha']);
When accessing the login (user, password) it will return a boolean. Then do it below the call.
if($retorno) {
echo 'Fez login';
else
echo 'Usuario ou senha invalidos';
Login method is this:
at the beginning of the page do the include where it has the method getConexao ();
function login($login, $senha) {
$login = mysql_escape_string($login);
$senha = mysql_escape_string($senha);
try {
$sql = "SELECT usuario FROM login WHERE Login='$login' AND Senha='$senha' LIMIT 1";
$conn = getConexao();
$result = $conn->query($sql);
$row = $result->fetch(PDO::FETCH_OBJ);
//faça um print_r no $row para fazer o retorno...
return true;
} catch (PDOException $e) {
return false;
}
}