SELECT operation in PHP with SQL Server 2008

Asked

Viewed 264 times

0

Starting in PHP with SQL Server 2008. I wonder 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 enter data in the database, but I’m not able to pull data and play in the variable $selects.

When I access the PHP page the following appears: inserir a descrição da imagem aqui

What’s wrong?

1 answer

1


When executing the command echo you are just printing the contents of the variable $seleciona, but this will not perform the query in the database let alone bring the desired result.

The correct is for you to first execute the query in the database through the appropriate command and then within a loop to display 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>';
}
  • Thank you very much friend, solved!

Browser other questions tagged

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