Single user registration and password converting mysql_* to PDO

Asked

Viewed 305 times

-3

I need to update a legacy code and for that I need to make two modifications to it.

The first change is to leave office mysql_* and use the PDO.

The second is to update the database so that I can not register two usernames and password with the same values.

Code seeking the Nome and imgPerfil of a user:

<?php 

require_once "includes/config.php";

$Usuario = $_SESSION["Usuario"];
$Senha   = $_SESSION["Senha"];

$SQL = mysql_query("SELECT Nome, imgPerfil FROM administradores WHERE Usuario=$Usuario' AND Senha='$Senha' ");

Code that takes user values:

<?php 

while($Linha = mysql_fetch_assoc($SQL)) {
    $nomeUser = $Linha['Nome'];
    $imgpUser = $Linha['imgPerfil'];
}

Code printing the values:

<?php echo $nomeUser; ?>

and

<?php echo $imgpUser; ?>
  • I can’t understand the question.

  • 2

    This tutorial shows how to use PDO, including Prepared Statements: http://www.ultimatephp.com.br/como-usar-pdo-com-banco-data

1 answer

4


To avoid duplicate values set the column as direct Unique key by the bank.

ALTER TABLE tabela ADD CONSTRAINT UNIQUE (campo)

To convert code with PDO and Prepared statements

$db = new PDO('mysql:host=localhost;dbname=teste',$usuario_db, $senha_db);

$sql = 'SELECT Nome, imgPerfil FROM administradores 
        WHERE Usuario= :usuario AND Senha =:senha';

$stmt = $db->prepare($sql);
$stmt->bindValue(':usuario', $usuario);
$stmt->bindValue(':senha', $senha);

$stmt->execute();
$item = $stmt->fetch(PDO::FETCH_ASSOC); //retorna apenas uma linha
echo $item['Nome'] .' - '. $item['imgPerfil'];

Browser other questions tagged

You are not signed in. Login or sign up in order to post.