I have 3 tables in a database (person, roles, rolesToPerson). The person table stores the person's information. The roles table has all the possible roles. The rolesToPerson table stores which roles each person has. I am trying to add a role to a person. Here is my code:

$tableName = "rolesToPerson";
$roleID = mysql_query("SELECT RoleID FROM roles WHERE RoleName = '$role'") or die(mysql_error);
$personID = mysql_query("SELECT PersonID FROM person WHERE userName = '$userName'") or die(mysql_error);
$roleToPersonQuery = "INSERT INTO $tableName (roleId, personId) VALUES ('$roleID', '$personID')";
mysql_query($roleToPersonQuery) or die(mysql_error());

here is the error I get:

Cannot add or update a child row: a foreign key constraint fails (`pink`.`rolestoperson`, CONSTRAINT `fk_RolesToPerson_Person1` FOREIGN KEY (`personId`) REFERENCES `person` (`personId`) ON DELETE NO ACTION ON UPDATE NO ACTION)

What I don't understand is that if I perform these queries myself through the mySQL console, everything works correctly. But when I run it this way it doesn't work.