简体   繁体   中英

SELECT* mssql + php

I wanna to show the results from this:

require 'connection.php';
$connectionInfo = array("UID" => $usr, "PWD" => $pwd, "Database" => $db);

$conn = sqlsrv_connect($serverName, $connectionInfo);
$tsql = "SELECT * FROM tadatable";    

/* Execute the query. */    

$stmt = sqlsrv_query( $conn, $tsql);    

if ( $stmt )    
{    
       $id = $stmt['id']; 
      echo"<td>".$stmt['name']."</td>"; 
}     echo"<td>".$stmt['name2']."</td>"; 
else     
{    
     echo "Error in statement execution.\n";    
     die( print_r( sqlsrv_errors(), true));    
}    

sqlsrv_free_stmt( $stmt);    
sqlsrv_close( $conn);

No data executed. Just blank page, can you please see what is wrong? MSSQL/PHP/ works fine...

sqlsrv_query
Returns a statement resource on success and FALSE if an error occurred.

After you've successfully executed the query with sqlsrv_query you can fetch the results, by using sqlsrv_fetch_array or use sqlsrv_fetch_array to get first result directly.

$stmt = sqlsrv_query( $conn, $tsql);   
if($stmt === false) {
    die( print_r( sqlsrv_errors(), true) );
}
// loop results with `sqlsrv_fetch_array`
while( $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC) ) {
    echo $row['id'].", ".$row['name'].", ".$row['name2']."<br />";
}

I feel you missed scrollable array.

Alter the following line.

$stmt = sqlsrv_query( $conn, $tsql, array(), array( "Scrollable" => 'static' ));
<?php  
require 'connection.php';

//$serverName = "(local)";

$connectionInfo = array("UID" => $usr, "PWD" => $pwd, "Database" => $db);  
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn === false ){  
 echo "Could not connect.\n";  
 die( print_r( sqlsrv_errors(), true));  
}  


$tsql = "SELECT * FROM tadatable";

if( sqlsrv_query( $conn, $tsql))  
{  
      //echo "Statement executed.";  

 while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
     echo $row['id'].", ".$row['name'].", ".$row['name2']."<br />";
   }

}   
else  
{  
      echo "Error in statement execution.\n";  
      die( print_r( sqlsrv_errors(), true));  
}  


sqlsrv_close($conn);  
?>  

For Reference : http://php.net/manual/en/function.sqlsrv-fetch-array.php

Example #1 Retrieving an associative array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM