O
O
okovalev2019-02-08 18:28:18
ubuntu
okovalev, 2019-02-08 18:28:18

Fatal error: Cannot redeclare def_result() why occurs only on pass 3 of the loop?

function def($tel, $mysqli) {
  $cheked_count = 8; //Количество успешных проверок МегаФон
        
    function def_result($tel, $mysqli) {
    $code = substr($tel, 1, 3);
    $number = substr($tel, -7);
      
    $sql = "SELECT `id`, `mf_operator_id`, `mf_region_id`, `mf_count`
    FROM `def` 
    WHERE `def_code` = '$code' 
    AND `def_from` <= '$number' 
    AND `def_to` >= '$number'";
    
    $result = $mysqli->query($sql);
    
    if($result->num_rows == 0){
      "Ошибка def: $sql\n";
      exit;
    }
    
    $def = $result->fetch_assoc();

    return $def;
    
  }
  
  function mf_api($tel) {
    $url = 'http://www.megafon.ru/api/mfn/info' . '?';
    $param = ['msisdn' => $tel];
    $mf_api = (array) json_decode(file_get_contents($url . http_build_query($param)));
    return $mf_api;
  }
  
  function base_intersect($def, $mf){
    //Если одинаковые то true, если разные то false
    if($def['mf_operator_id'] == $mf['operator_id'] 
      && $def['mf_region_id'] == $mf['region_id'] ) {
      return true;			
    }else {	return false; }
  }
  
  $def = def_result($tel, $mysqli);
  
  if($def['mf_count'] > $cheked_count) {
    return [
      'tel' => $tel,
      'operator_id' => $def['mf_operator_id'],
      'region_id' => $def['mf_region_id']
    ];
  }else {
    $mf = mf_api($tel);
    
    if(base_intersect($def, $mf)) {
      $sql = "UPDATE `def` SET `mf_count` = `mf_count` + 1 WHERE `id` = " . $def['id'];
    }else {
      $sql = "UPDATE `def` SET 
      `mf_operator`= '$mf[operator]',`mf_operator_id` = '$mf[operator_id]',
      `mf_region` = '$mf[region]',`mf_region_id` = '$mf[region_id]',
      `mf_count` = 0
      WHERE `id` = " . $def['id'];
    }
    $mysqli->query($sql);
    
    return [
      'tel' => $tel,
      'operator_id' => $mf['operator_id'],
      'region_id' => $mf['region_id']
    ];
  }
}

Why does this function cause an error on the 3rd pass of the loop?
Fatal error : Cannot redeclare def_result() (previously declared in C:\xampp\htdocs\bank\console\localhost\defcode\.testphp:48) in C:\xampp\htdocs\bank\console\localhost\defcode\test. php line 48

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question