SELECT operation in PHP with SQL Server 2008

0

Starting in PHP with SQL Server 2008. I'd like to know what's wrong with my code.

<?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 numero FROM usuarios WHERE numero=4";

echo "Opa $seleciona ";

I was able to insert data into the database, but I am not able to pull data and play in the variable $ select .

When I access the PHP page the following appears:

What's wrong?

    
asked by anonymous 29.04.2018 / 22:18

1 answer

1

When executing the command echo you are only printing the content of the $seleciona variable, but this will not perform the query in the database, much less bring the desired result.

The correct one is you first execute the query in the database through the appropriate command and then within a loop repeat the desired content, below an example of what should be used in place of your echo :

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

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