Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中获取MS SQL中的插入ID时出现问题_Php_Sql Server_Sql Server 2008 R2 - Fatal编程技术网

在PHP中获取MS SQL中的插入ID时出现问题

在PHP中获取MS SQL中的插入ID时出现问题,php,sql-server,sql-server-2008-r2,Php,Sql Server,Sql Server 2008 R2,好的,我正在使用hostgator和我的正常函数来获取插入id,但是没有工作,他们向我保证这是一个代码问题。我在MS SQL上使用这些函数时没有任何问题。所以我得找个工作 我想知道的是,使用两个单独的查询来获取插入的ID是否存在任何问题?如果另一个用户在这两个查询之间插入同一个表,是否会出现问题?请参阅下面我的功能。 /** * Inserts the sql query and returns the ID of the Row Inserted. * * @param string $

好的,我正在使用hostgator和我的正常函数来获取插入id,但是没有工作,他们向我保证这是一个代码问题。我在MS SQL上使用这些函数时没有任何问题。所以我得找个工作

我想知道的是,使用两个单独的查询来获取插入的ID是否存在任何问题?如果另一个用户在这两个查询之间插入同一个表,是否会出现问题?请参阅下面我的功能。

/**
 * Inserts the sql query and returns the ID of the Row Inserted.
 *
 * @param string $IncomingSql The sql query that you want to execute.
 * @return int/string returns the ID of inserted row, or 0 if no result.
 */
function dbInsert($_IncomingSql)
{
    $sql=$_IncomingSql.'; SELECT SCOPE_IDENTITY();';
    $Result=dbQuery($sql);
    sqlsrv_next_result($Result);
    sqlsrv_fetch($Result);

    $stmt = sqlsrv_get_field($Result, 0);
    if($stmt >0)
    {    
        return $stmt;
    }
    else {
        return 0;
    }
}
function dbInsert($_IncomingSql)
{
    $sql=$_IncomingSql.'; SELECT SCOPE_IDENTITY();';
    $Result=dbQuery($sql);
  if($Result)
  {
    $stmt = dbStr('SELECT SCOPE_IDENTITY();');

    if($stmt >0)
    {    
        return $stmt;
    }
    else {
        return 0;
    }
  }
}
/**
 * This function is designed to catch SQL errors and dispese to the appropriate channels.
 * It can send error emails or display on screen at the time of error.
 * All functions accessing the database need to go through this function in order to catch errors in a standardized way for all pages.
 *
 * @param string $_IncomingSql The sql query that you want to execute.
 * @Param string $_Cursor OPTIONAL PARAMETER - This is the cursor type for scrolling the result set. 
 * @Return resource/bool
 */
function dbQuery($_IncomingSql)
{
    $Result=sqlsrv_query(CONN, $_IncomingSql);
    //Catch sql errors on query
    if($Result===false) {
        if(($errors=sqlsrv_errors())!=null) {
            CatchSQLErrors($errors, $_IncomingSql);
        }
    }
    return $Result;
}



/**
 *
 * Executes the $ImcomingSql query and returns the first cell in the first row
 *
 * @param string $_IncomingSql The sql query that you want to execute.
 * @param string $_DefaultValue The value to return if null.
 */
function dbStr($_IncomingSql, $_DefaultValue=0)
{
    $Result=dbQuery($_IncomingSql);
    if($Result!=0) {
        $Rows=sqlsrv_has_rows($Result);
        if($Rows) {
            $Row=sqlsrv_fetch_array($Result, SQLSRV_FETCH_NUMERIC);
            $Result=$Row[0];
            return $Result;
        }
    }
    return $_DefaultValue;
}
这是我获取插入id的常规函数(在主机gator共享服务器上每次返回0。插入工作正常。但sqlsrv\u get\u fiedl每次返回false)。

这是我用来获取插入ID的函数。

/**
 * Inserts the sql query and returns the ID of the Row Inserted.
 *
 * @param string $IncomingSql The sql query that you want to execute.
 * @return int/string returns the ID of inserted row, or 0 if no result.
 */
function dbInsert($_IncomingSql)
{
    $sql=$_IncomingSql.'; SELECT SCOPE_IDENTITY();';
    $Result=dbQuery($sql);
    sqlsrv_next_result($Result);
    sqlsrv_fetch($Result);

    $stmt = sqlsrv_get_field($Result, 0);
    if($stmt >0)
    {    
        return $stmt;
    }
    else {
        return 0;
    }
}
function dbInsert($_IncomingSql)
{
    $sql=$_IncomingSql.'; SELECT SCOPE_IDENTITY();';
    $Result=dbQuery($sql);
  if($Result)
  {
    $stmt = dbStr('SELECT SCOPE_IDENTITY();');

    if($stmt >0)
    {    
        return $stmt;
    }
    else {
        return 0;
    }
  }
}
/**
 * This function is designed to catch SQL errors and dispese to the appropriate channels.
 * It can send error emails or display on screen at the time of error.
 * All functions accessing the database need to go through this function in order to catch errors in a standardized way for all pages.
 *
 * @param string $_IncomingSql The sql query that you want to execute.
 * @Param string $_Cursor OPTIONAL PARAMETER - This is the cursor type for scrolling the result set. 
 * @Return resource/bool
 */
function dbQuery($_IncomingSql)
{
    $Result=sqlsrv_query(CONN, $_IncomingSql);
    //Catch sql errors on query
    if($Result===false) {
        if(($errors=sqlsrv_errors())!=null) {
            CatchSQLErrors($errors, $_IncomingSql);
        }
    }
    return $Result;
}



/**
 *
 * Executes the $ImcomingSql query and returns the first cell in the first row
 *
 * @param string $_IncomingSql The sql query that you want to execute.
 * @param string $_DefaultValue The value to return if null.
 */
function dbStr($_IncomingSql, $_DefaultValue=0)
{
    $Result=dbQuery($_IncomingSql);
    if($Result!=0) {
        $Rows=sqlsrv_has_rows($Result);
        if($Rows) {
            $Row=sqlsrv_fetch_array($Result, SQLSRV_FETCH_NUMERIC);
            $Result=$Row[0];
            return $Result;
        }
    }
    return $_DefaultValue;
}
上述函数中使用的函数。

/**
 * Inserts the sql query and returns the ID of the Row Inserted.
 *
 * @param string $IncomingSql The sql query that you want to execute.
 * @return int/string returns the ID of inserted row, or 0 if no result.
 */
function dbInsert($_IncomingSql)
{
    $sql=$_IncomingSql.'; SELECT SCOPE_IDENTITY();';
    $Result=dbQuery($sql);
    sqlsrv_next_result($Result);
    sqlsrv_fetch($Result);

    $stmt = sqlsrv_get_field($Result, 0);
    if($stmt >0)
    {    
        return $stmt;
    }
    else {
        return 0;
    }
}
function dbInsert($_IncomingSql)
{
    $sql=$_IncomingSql.'; SELECT SCOPE_IDENTITY();';
    $Result=dbQuery($sql);
  if($Result)
  {
    $stmt = dbStr('SELECT SCOPE_IDENTITY();');

    if($stmt >0)
    {    
        return $stmt;
    }
    else {
        return 0;
    }
  }
}
/**
 * This function is designed to catch SQL errors and dispese to the appropriate channels.
 * It can send error emails or display on screen at the time of error.
 * All functions accessing the database need to go through this function in order to catch errors in a standardized way for all pages.
 *
 * @param string $_IncomingSql The sql query that you want to execute.
 * @Param string $_Cursor OPTIONAL PARAMETER - This is the cursor type for scrolling the result set. 
 * @Return resource/bool
 */
function dbQuery($_IncomingSql)
{
    $Result=sqlsrv_query(CONN, $_IncomingSql);
    //Catch sql errors on query
    if($Result===false) {
        if(($errors=sqlsrv_errors())!=null) {
            CatchSQLErrors($errors, $_IncomingSql);
        }
    }
    return $Result;
}



/**
 *
 * Executes the $ImcomingSql query and returns the first cell in the first row
 *
 * @param string $_IncomingSql The sql query that you want to execute.
 * @param string $_DefaultValue The value to return if null.
 */
function dbStr($_IncomingSql, $_DefaultValue=0)
{
    $Result=dbQuery($_IncomingSql);
    if($Result!=0) {
        $Rows=sqlsrv_has_rows($Result);
        if($Rows) {
            $Row=sqlsrv_fetch_array($Result, SQLSRV_FETCH_NUMERIC);
            $Result=$Row[0];
            return $Result;
        }
    }
    return $_DefaultValue;
}

在$stmt=sqlsrv\u get\u字段($Result,1)中使用“1”而不是“0”是否正确?请参阅No,my bad中的示例,我们在使用主机gator进行调试时对其进行了更改。我已按原样将其设置回0。作为调试的一种形式,能否先打印出完整的结果集?不知INSERT语句是否生成某种记录集,导致sqlsrv_get_field()在SCOPE_IDENTITY()之前返回某些内容。@Greg在INSERT函数中打印dbQuery的结果?不知您是否返回了多个数据集(一个用于插入,一个用于查询),因此,请查看是否可以在调试会话中迭代或循环整个数据集。