简体   繁体   English

选择* mssql + php

[英]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... MSSQL / PHP /正常工作...

sqlsrv_query sqlsrv_query
Returns a statement resource on success and FALSE if an error occurred. 返回成功的语句资源,如果发生错误则返回FALSE。

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. 使用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 />";
}

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 供参考: http : //php.net/manual/zh/function.sqlsrv-fetch-array.php

Example #1 Retrieving an associative array. Example#1检索一个关联数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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