I would like to insert a random value from a php array into a mysql table.
My php array is $profile_outM = (3,6,7)
The following command works nicely and sets user_profile
to 7 for all users having user_UFR
equal to 2:
UPDATE `users` SET `user_profile` = '$profile_outM[3]' WHERE `user_UFR`= 2
But how can I select a random value from $profile_outM
?
Best,
HERE is the solution (might not be very elegant / efficient) but it works:
Starting with
$profile_outM
as a stringI convert to an array and get the number of element
profile2array = explode(",", $profile_outM);
$length_profile2array = count($profile2array);
- Then
"SET @myValues = '$profile_outM'"
"UPDATE `users` SET `user_profile` = SUBSTRING_INDEX(REVERSE(SUBSTRING_INDEX(@Values, ',', FLOOR(1+RAND()*$length_profile2array))),',',1) WHERE `user_UFR`=2"
- This way, all users get a different value.
W