Ok, so I am using hostgator and my normal function to get the insert id is not working and they assure me that its a code problem. I use these functions at work on MS SQL with no issues at all. So I have to find a work around.
What I would like to know is, is there any issue using two separate queries to get the inserted ID? If another user makes an insert into the same table in between those two queries, will there be issues? Please see my functions below.
This is my normal function to get the inserted id. (Which returns 0 every time on host gator shared server. The insert works properly. But the sqlsrv_get_fiedl returns false every time.)
/**
* 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, 1);
if($stmt >0)
{
return $stmt;
}
else {
return 0;
}
}
This is the function I am having to use to get the inserted ID.
function dbInsert($_IncomingSql)
{
$sql=$_IncomingSql.'; SELECT SCOPE_IDENTITY();';
$Result=dbQuery($sql);
$stmt = dbStr('SELECT SCOPE_IDENTITY();');
if($stmt >0)
{
return $stmt;
}
else {
return 0;
}
}
The functions that are used in the above functions.
/**
* 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;
}
Aucun commentaire:
Enregistrer un commentaire