I have a MySQL table with an integer column in it called count
which I'm trying to do some basic math on and then update the value. Here is an example of one such task I'm trying to do.
$sql = 'SELECT count FROM triggers WHERE id = ?';
$params = array($id);
$result = sqlsrv_query($connection, $sql, $params);
$row = sqlsrv_fetch_array($result);
$count = intval($row[0]);
if ($count = 0)
return -1;
$newCount = $count - 1;
$sql = 'UPDATE triggers SET count = ? WHERE id = ?';
$params = array($newCount, $id);
$result = sqlsrv_query($connection, $sql, $params);
The problem I'm having is that $newCount
always equals -1, so the database value of count
is updated to -1. In one particular example run, $count
was equal to 84, so it passed the if statement. But then instead of $newCount
being set to 83, it gets set to -1.
I have a feeling this has something to do with the MySQL value being treated as a string instead of an integer, but I thought intval()
would solve that. I also tried using (int)
instead, but no difference.