SELECT * mssql + php

时间:2017-06-20 08:55:57

标签: php sql-server database

我想从中显示结果:

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

没有执行任何数据。只是空白页,你能看到有什么问题吗? MSSQL / PHP /工作得很好......

3 个答案:

答案 0 :(得分:0)

  

<强> sqlsrv_query
  成功时返回语句资源,如果是,返回FALSE   发生错误。

使用sqlsrv_query成功执行查询后,您可以使用sqlsrv_fetch_array或使用sqlsrv_fetch_array直接获取结果来获取结果。

$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 />";
}

答案 1 :(得分:-1)

我觉得你错过了可滚动的数组。

更改以下行。

$stmt = sqlsrv_query( $conn, $tsql, array(), array( "Scrollable" => 'static' ));

答案 2 :(得分:-1)

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

供参考: http://php.net/manual/en/function.sqlsrv-fetch-array.php

     

示例#1检索关联数组。