First i must say this my second question (first was closed becouse i did not explain my question very good)
I am building databse that should be used by our 50-60 employes. In our base we have many customers (1row = 1customer). Idea is when employe open page, it should display one customer, but if another employe open same page it should show another customer (i want to avoid situations if 2 employe press refresh button at the same time, the page sould not display same customer.
i have this php code for getting data out of base, but if i push refresh on same time it will give me same result on 2 PC. How can i avoid that
In table telesales_anc i have column 'Lock_ID' and php update it with '1' when page is open
Is there any function that 'hold' the connection to the row or something like that? I try to google it but i only find some solution with timer (but i think this is a bad idea, and still 2 emlpoyes can open in same time)
$MSISDN = "SELECT MSISDN FROM telesales_anc WHERE Lock_ID != 1 LIMIT 1";
$result = $mysqli->query($MSISDN);
while ( $row = $result->fetch_assoc() ) {
$IDcheck = "SELECT ID FROM telesales_anc WHERE '{$row['MSISDN']}' = MSISDN";
$result_IDcheck = $mysqli->query($IDcheck);
while ( $row = $result_IDcheck->fetch_assoc()) {
$lock_ID = "UPDATE telesales_anc SET Lock_ID = 1 where '{$row['ID']}' = ID";
$result_ID = $mysqli->query($lock_ID);
}}
I add a usleep() for a very short time to wait other users lock and a small change at the 2nd sql AND Lock_ID!=1
so if other user locked it you wont fetch this one, please be aware that you need some loops for retries to fetch unlocked one
$MSISDN = "SELECT MSISDN FROM telesales_anc WHERE Lock_ID != 1 LIMIT 1";
$result = $mysqli->query($MSISDN);
usleep(rand(100000,300000)); // we are sleeping randomly between 0.1 - 0.3 seconds for waiting the other users lock
while ( $row = $result->fetch_assoc() ) {
$IDcheck = "SELECT ID FROM telesales_anc WHERE '{$row['MSISDN']}' = MSISDN AND Lock_ID != 1";
$result_IDcheck = $mysqli->query($IDcheck);
while ( $row = $result_IDcheck->fetch_assoc()) {
$lock_ID = "UPDATE telesales_anc SET Lock_ID = 1 where '{$row['ID']}' = ID";
$result_ID = $mysqli->query($lock_ID);
}}