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

时间:2015-07-08 13:47:17

标签: php sql

这是我的代码。错误消息表明问题出在while循环语句中。

  

警告:sqlsrv_fetch_array()期望参数1为资源,在第136行中给出null

/*  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) ) {

所有这一切的奇怪部分是我的代码仍然会编译。即使我从客户端的角度更改了某些内容,它也会在数据库中进行更改并列出新信息。我只需要错误消失!

谢谢你们!

1 个答案:

答案 0 :(得分:0)

请参阅PHP docu

中的示例1
<?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));
}
?>

在使用之前,您需要检查$ stmt是否为false(使用===或!==而不是==或!=)!