duaiwu8385 2015-07-08 13:47
浏览 69

Php服务器调用工作但仍然收到错误消息

Here is my code. The error message says the problem is in the while loop statement.

Warning: sqlsrv_fetch_array() expects parameter 1 to be resource, null given in line 136

/*  We need to create a connection to SQLServer08 */
$serverName = "**************\main, 1433"; 
$connectionOptions = array("Database"=>"****", "UID"=>"****","PWD"=>"****");
$conn = sqlsrv_connect( $serverName, $connectionOptions);

$EventID = $_GET['id'];

/*  Excecute a Query   */   
$tsql = "SELECT *
        ,convert(int,LEFT(starttime,2)) as StartHour
        ,SUBSTRING(convert(varchar,StartTime),4,2) as StartMinute
        ,convert(int,LEFT(endtime,2)) as EndHour
        ,SUBSTRING(convert(varchar,EndTime),4,2) as EndMinute 
        FROM Event where EventID =". $EventID; 
$stmt = sqlsrv_query( $conn, $tsql); 

/* Retrieve and display the results of the query. This is an array-recordset, so we loop thru the results */

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {

The weird part with all of this is that my code will still compile. Even when I change something from the client's perspective, it makes the change in the database and lists the new information. I just need the error to go away!

Thanks guys!

  • 写回答

1条回答 默认 最新

  • doulin6448 2015-07-08 15:19
    关注

    See the example 1 from the PHP docu

    <?php
    $serverName = "serverName\sqlexpress";
    $connectionInfo = array( "Database"=>"dbName", "UID"=>"username", "PWD"=>"password" );
    $conn = sqlsrv_connect( $serverName, $connectionInfo);
    if( $conn === false ) {
         die( print_r( sqlsrv_errors(), true));
    }
    
    $sql = "INSERT INTO Table_1 (id, data) VALUES (?, ?)";
    $params = array(1, "some data");
    
    $stmt = sqlsrv_query( $conn, $sql, $params);
    if( $stmt === false ) {
         die( print_r( sqlsrv_errors(), true));
    }
    ?>
    

    You need to check $stmt for false (use === or !== instead of == or !=) before using it!

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大