Pulling data from SQL Server using PHP

Asked

Viewed 340 times

-2

Hello folks using PHP code below I can insert rows in 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 the corresponding login of that number is not showing.

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

Appears: The name is: Resource id #3

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

Then you should show up: The name is Joao

  • Is the number field integer? If not, put 8 inside single quotes

  • Yeah, he’s like an account ID

  • You don’t understand the doubt you are having. A barely explicit login and password. In the query puts everything you want to "pull" from SQL

  • I can enter, but I couldn’t select

1 answer

0

Follow this example, which will help you solve your problem, you just need to change the server name and the instance of sql server, as well as the user and the server access password.

Either way, you’re storing information that’s not encrypted, and a serious security problem, but this was a separate.

<?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);
?> 

Browser other questions tagged

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