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?
If you have an issue with the ID being a string (in most cases, it isn’t a big deal), then simply use
intval():or
Why’s it a string?
mysql_fetch_assocreturns all values as astring, regardless of the type in the database. Straight from PHP documentation: