Compares sqlsrv_query with text

0

I'm starting PHP with SQL Server 2008.

The code below makes a query in the database and displays what has been obtained.

<?php
$serverName = "DESKTOP-B8EB4SG\SQLEXPRESS";
$connectionInfo = array( "Database"=>"contas", "UID"=>"sa", "PWD"=>"123456" );
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false ) {
     die( print_r( sqlsrv_errors(), true));
}

//$sql = "INSERT INTO usuarios (login, senha) VALUES (?, ?)";


$seleciona = "SELECT Login FROM pessoas WHERE UID=3";

// Executa a consulta
$stmt = sqlsrv_query($conn, $seleciona);
if ($stmt === false) {
    die(print_r(sqlsrv_errors(), true));
}


if ($stmt == "renato")
echo "Bem-vindo";

// Exibe o resultado
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
    echo $row['Login'].'<br>';
}       


?>

I would like that when the Login name obtained was renato , it displayed Welcome . After all, why does not the welcome appear? Do you need some sort of conversion to string / char?

NOTE: I know there must be endless explanations about this, but I do not know exactly what to look for.

    
asked by anonymous 03.05.2018 / 00:33

1 answer

0

You can if within while :

// Exibe o resultado
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {

    if ($row['Login'] == "renato")
         echo 'Bem-vindo'.'<br>';
    else
         echo $row['Login'].'<br>';
}       

In this way, it checks to see if it is renato , if it does, it prints "Bem-vindo" , and if not, it prints the return.

    
05.05.2018 / 13:09