Pulling SQL Server data using PHP

-2

Hello everyone using the PHP code below I can insert rows into my SQL Server table:

sqlsrv_query( $conn, "INSERT INTO usuarios (login, senha) VALUES ('new', 'mano')");

However, I would like to return some value and play in a text. I tried the command below but it does not appear the login corresponding to that number.

$stmt = sqlsrv_query( $conn, "SELECT login FROM usuarios WHERE numero = 8");

Appears: The name is: Resource id # 3

Since in the database, the number 8 login is Joao.

Then it should appear: The name is: Joao

    
asked by anonymous 30.04.2018 / 02:31

1 answer

0

This example, which will help you solve your problem, you just have to change the name of the server and the sql server instance, as well as the username and password to access the server.

In any case you are saving information without being encrypted, and a serious security problem, but this was a separate one.

<?php
$serverName = "servidor\instancia";
$connectionInfo = array( "Database"=>"bancodedados", "UID"=>"username", "PWD"=>"password" );
$conn = sqlsrv_connect( $serverName, $connectionInfo );
if( $conn === false ) {
    die( print_r( sqlsrv_errors(), true));
}

$sql = "SELECT login FROM usuarios WHERE numero = 8";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
    die( print_r( sqlsrv_errors(), true) );
}

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC) ) {
      echo $row[0] . "<br />";
}

sqlsrv_free_stmt( $stmt);
?> 
    
02.05.2018 / 18:20