bindValue('key', bin2hex(random_bytes(64))); $stm -> execute(); $key_id = $db -> lastInsertRowID(); $stm -> close(); return $key_id; } function get_value_id_of($machine, $state) { if (is_int($machine) && is_int($state)) { $stm = $db -> prepare( 'SELECT "values".id as value_id FROM "values"' . ' WHERE "values".state_id = :state_id' . ' AND "values".machine_id = :machine_id' ); $stm -> bindValue('state_id', $state); $stm -> bindValue('machine_id', $machine); } else { $stm = $db -> prepare( 'SELECT "values".id as value_id FROM "values"' . ' JOIN states ON states.id = "values".state_id' . ' JOIN machines ON machines.id = "values".machine_id' . ' WHERE states.name = :state' . ' AND machines.name = :machine' ); $stm -> bindValue('state', $state); $stm -> bindValue('machine', $machine); } $result = $stm -> execute(); if (!$result) die(); $result = $result -> fetchArray(); if (!$result) die(); $stm -> close(); return $result['value_id']; } function add_key_for($machine, $state) { $key_id = add_key(); $value_id = get_value_id_of($machine, $state); $stm = $db -> prepare( 'INSERT INTO permissions (key_id, value_id) VALUES (:key,:value)'); $stm -> bindValue('value', $value_id); $stm -> bindValue('key', $key_id); $stm -> execute(); $stm -> close(); } } $db = new MyDB(); if(!$db) { echo $db->lastErrorMsg(); die(); }