Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.76 KB | None | 0 0
  1. <?php
  2.  
  3. function query($mysqli,$sql,$a_params) {
  4.        
  5.     if (count($a_params)==0) {
  6.  
  7.         $stmt=($mysqli)->prepare($sql);
  8.         $stmt->execute();
  9.     }
  10.     else {
  11.         $stmt = $mysqli->prepare($sql);
  12.         if($stmt === false) {
  13.             trigger_error('Wrong SQL: ' . $sql . ' Error: ' . ($mysqli)->errno . ' ' . ($mysqli)->error, E_USER_ERROR);
  14.         }
  15.  
  16.         /* use call_user_func_array, as $stmt->bind_param('s', $param); does not accept params array */
  17.         call_user_func_array(array($stmt, 'bind_param'), refValues($a_params));
  18.  
  19.         /* Execute statement */
  20.         $stmt->execute();
  21.  
  22.     }
  23.     $res = $stmt->get_result();
  24.        
  25.  
  26.  
  27.     return $res;
  28. }
  29.  
  30. function refValues($arr)
  31.     {
  32.         if (strnatcmp(phpversion(),'5.3') >= 0) //Reference is required for PHP 5.3+
  33.         {
  34.             $refs = array();
  35.             foreach($arr as $key => $value)
  36.                 $refs[$key] = &$arr[$key];
  37.             return $refs;
  38.         }
  39.         return $arr;
  40.     }
  41.  
  42. //include $_SERVER['DOCUMENT_ROOT']."/include/config.php";
  43.  
  44. $json_array  = file_get_contents('https://api.exline.systems/public/v1/regions/origins?country=KZ');
  45.  
  46.  
  47. $php_array = json_decode( $json_array , true );
  48. if (!is_array($php_array)) die('Json convert error');
  49.  
  50. $regions=$php_array['regions'];
  51.  
  52.  
  53. foreach ( $regions as $row) {
  54.         //var_dump($row);
  55.        
  56.        
  57.         query($__ссылка_на_mysqli__,
  58.             "INSERT INTO table_json (
  59.                 id,
  60.                 title,
  61.                 cached_path
  62.                 zone
  63.                 origin
  64.                 destination
  65.                 cached_parent
  66.             ) VALUES (?,?,?,?,?,?,?)", array(
  67.                 "isssiis",
  68.                 $row['id'],
  69.                 $row['title'],
  70.                 $row['cached_path'],
  71.                 $row['zone'],
  72.                 $row['origin'],
  73.                 $row['destination'],
  74.                 $row['cached_parent'],
  75.             )
  76.         );
  77.        
  78.       //$mysql_query = "insert into `table_json` (`".implode( '`,`',$fields). "`) values ('".implode( "','",$values)."')";
  79.      
  80.       //echo $mysql_query;
  81.      
  82. }
  83. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement