MySql Conversion To PDO

-2
@$sql_usuario = "SELECT * FROM usuario ";
@$qry_usuario  = @mysql_query($sql_usuario );
@$linha_usuario  = mysql_fetch_array($qry_usuario );

Array ([0] => 6 [u_id] => 6 [1] => 54354554 [u_cod] => 54354554 [2] => 54354554 [u_cod] => 54354554) = 17 [u_id] => 17 [1] => 3232323 [u_cod2] => 3232323 [2] => 3232323 [u_cod2] => 3232323

I have this query . How would I convert to PDO ? The return of the data is different:

@$sql_usuario  = $dbconn->prepare("SELECT * FROM usuario");
@$sql_usuario->execute();
@$linha_usuario = $sql_usuario->fetch(PDO::FETCH_ASSOC);
> [u_id] => 6 [u_cod] => 54354554 [u_cod] => 54354554) Array ([u_id] => 17 [u_cod2] => 3232323 [u_cod2] = & 32t32323 )

    
asked by anonymous 29.11.2018 / 15:34

2 answers

0

Try the following in PDO:

@ $ sql_user = $ dbconn-> prepare ("SELECT * FROM user"); @ $ sql_user-> execute (); @ $ rows = $ sql_user-> fetchAll (PDO :: FETCH_ASSOC);

    
29.11.2018 / 15:45
0

I will post a very simple example, which will easily convert, so that you learn every detail:

File: config.php

date_default_timezone_set('America/Sao_Paulo');
header('Content-Type: text/html; charset=utf-8');

$user = 'root'; # Usuário do banco de dados
$pswd = '123';  # Senha

# Tentativa de conexão
try {
    $conn = new PDO('mysql:host=192.168.0.1; dbname=nomedobanco', $user, $pswd);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

} catch(PDOException $e) {

    echo 'ERROR: ' . $e->getMessage();
}

File: login.php

ini_set('default_charset', 'utf-8');
require_once('config.php');

$sql = "SELECT * FROM usuarios";
$statement = $conn->prepare($sql);
$statement->execute();

# Verifica se tem ao menos 1 linha
if($statement->rowCount()) {

    # Imprimindo o retorno
    $stt = $statement->fetch(PDO::FETCH_ASSOC);
    print_r($stt);

} else {

   echo "Sem dados.";
}
    
29.11.2018 / 15:50