I'm an Android developer that has next to no knowledge with php. I've an app that uses Google push notifications and works fine. The problem i'm having is when the same phone registers with Google that regID from google must be stored in my DB. If i push the app on to the phone say 4 times then i will have 4 rows in the DB pointing to one phone.
I'm trying to check the DB to see if a record exists for a given regID. I'm trying to use sqlsrv_has_rows() to do this. The code below compiles but does not insert a record if the record exists or not.
Can anyone see what the problem is?
Thanks in advance,
matt
public function storeUser($companyid, $gcm_regid) {
$strCompanyID = strval($companyid);
$strRegID = strval($gcm_regid);
$serverName = "LOCALHOST\SQLEXPRESS";
$uid = "gcm";
$pwd = "gcm";
$databaseName = "gcm";
$connectionInfo = array(
"UID" => $uid,
"PWD" => $pwd,
"Database" => $databaseName
);
$db = sqlsrv_connect($serverName, $connectionInfo)
or die("Unable to connect to server");
$query = "
SELECT *
FROM GcmUsers
WHERE gcuRegID = " . $strRegID;
$resultQueryRegID = sqlsrv_query($db, $query);
if ($resultQueryRegID) {
$rows = sqlsrv_has_rows($resultQueryRegID);
if ($rows === true) {
//echo "There are rows. <br />";
} else {
// echo "There are no rows. <br />";
$queryInsert = "
INSERT INTO GcmUsers
(gcuCompanyID, gcuRegID)
VALUES
('$strCompanyID','$strRegID')
";
$result = sqlsrv_query($db, $queryInsert);
}
}
}
[EDIT 1]
public
function storeUser($companyid, $gcm_regid) {
$strCompanyID = strval($companyid);
$strRegID = strval($gcm_regid);
$serverName = "LOCALHOST\SQLEXPRESS";
$uid = "gcm";
$pwd = "gcm";
$databaseName = "gcm";
$connectionInfo = array("UID" => $uid, "PWD" => $pwd, "Database" => $databaseName);
$db = sqlsrv_connect($serverName, $connectionInfo) or die("Unable to connect to server");
$sql = "SELECT * FROM GcmUsers where gcuRegID = ?";
// Initialize parameters and prepare the statement.
// Variables $qty and $id are bound to the statement, $stmt.
$stmt = sqlsrv_prepare($db, $sql, array(&$strRegID));
if (!$stmt) {
die(print_r(sqlsrv_errors(), true));
}
$result = sqlsrv_execute($stmt);
$rows = sqlsrv_has_rows($result);
if ($rows === true) {
// echo "There are rows. <br />";
} else {
// echo "There are no rows. <br />";
$queryInsert = "INSERT INTO GcmUsers ( gcuCompanyID, gcuRegID) values ('$strCompanyID','$strRegID')";
$result = sqlsrv_query($db, $queryInsert);
}
}
[edit2]
$sql = "SELECT * FROM GcmUsers where gcuRegID = ?";
// Initialize parameters and prepare the statement.
// Variables $qty and $id are bound to the statement, $stmt.
$stmt = sqlsrv_prepare( $db, $sql, array( &$strRegID));
if( !$stmt ) {
die( print_r( sqlsrv_errors(), true));
}
$result = sqlsrv_execute( $stmt );
if (sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {
// Got rows
}else{
// Not rows
$queryInsert = "INSERT INTO GcmUsers ( gcuCompanyID, gcuRegID) values ('$strCompanyID','$strRegID')";
$result = sqlsrv_query($db, $queryInsert);
}