Compares sqlsrv_query with text

Asked

Viewed 27 times

0

I am starting in PHP together with SQL Server 2008.

The code below makes a query in the database and displays what was 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>';
}       


?>

Cade o bem-vindo??

I would like when the login name obtained was re-birth, he displayed Welcome. After all, why doesn’t the welcome appear? You might need some kind of string/char conversion?

NOTE: I know there must be infinite explanations about this, but I don’t know exactly what to research.

  • $stmt is a Resource and will never equal a string like renato. Don’t go in that if inline.

  • What can I do?

  • Can do echo 'bem vindo '. $row['Login'].'<br>';

  • Only the welcome appeared

1 answer

0

You can do the if within the 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>';
}       

That way, he checks if it is renato, if it is, print "Bem-vindo", and if not, print the return.

Browser other questions tagged

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