使用PHP

时间:2015-06-28 21:55:19

标签: php sql-server sql-server-2008-r2

好的,所以我使用hostgator和我的普通函数来获取插入ID不起作用,他们向我保证它的代码问题。我在MS SQL上使用这些函数完全没有问题。所以我必须找到一个解决方法。

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

这是我获取插入ID的常规功能。 (每次在主机gator共享服务器上返回0。插入工作正常。但sqlsrv_get_fiedl每次都返回false。)

/**
 * 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;
    }
}

这是我必须使用的功能来获取插入的ID。

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

0 个答案:

没有答案