Reputation: 32061
I have a table contacts
that has a field arrayOfMembers
(JSON format) and a table users
with field userId
(int).
$recipientId=getUserIdForEmail($recipientEmail);
addUserIdToContactsMemberIds($noteId, $recipientId);
function addUserIdToContactsMemberIds($noteId, $userId)
{
$memberData=mysql_query("SELECT * FROM contacts WHERE contactId='$contactId'");
$memberResultData = mysql_fetch_assoc($memberData);
$arrayOfMemberIds=json_decode($memberResultData['arrayOfMembers']);
$arrayOfMemberIds[]=$userId;
$sendback=json_encode($arrayOfMemberIds);
mysql_query("UPDATE notes SET arrayOfMembers='$sendback' where contactId='$contactId'");
}
function getUserIdForEmail($email)
{
$data=mysql_query("SELECT userId FROM users WHERE userEmail='$email'");
$result = mysql_fetch_assoc($data);
return $result["userId"];
}
However, the $userId
is being stored as a string with quotations, so that where if I originally have [1], after adding a new userId, it looks like this [1,"2"] even though it should look like [1,2].
If I manually enter an int value in the line: $arrayOfMemberIds[]=$userId;
and put $arrayOfMemberIds[]=5
, then I get the correct result, that is [1,5]. Why is this happening?
Upvotes: 2
Views: 61
Reputation: 18785
If you have an issue with the ID being a string (in most cases, it isn't a big deal), then simply use intval()
:
$arrayOfMemberIds[] = intval($userId);
or
return intval($result["userId"]);
mysql_fetch_assoc
returns all values as a string
, regardless of the type in the database. Straight from PHP documentation:
Returns an associative array of strings that corresponds to the fetched row, or FALSE if there are no more rows. - http://php.net/mysql_fetch_assoc
Upvotes: 4