title: How to execute SQL query stored in variable in PHP
body: <p>I'm trying to run a query in PHP using <code>mysqli_query()</code></p>
  
  <p>My query is stored in variable <code>$query</code> as follows:</p>
  
  <blockquote>
  <pre><code>  $query = "INSERT INTO spaces (";
    $keys = '';
      $values = '';
      foreach ($newrow as $key=&gt;$value){
          $keys .= $key.",";
          $values .= "'".$value."',";
      }
      $keys = substr($keys, 0, -1);
      $values = substr($values, 0, -1);
      $query .= $keys;
      $query .= ") values(";
      $query .= $values;
      $query .= ")";`
  </code></pre>
  </blockquote>
  
  <p>On using <code>echo $query</code>, it shows the generated query. </p>
  
  <p>However, I must now execute this query in the database.</p>
  
  <p>Currently, I'm using <code>$result = mysqli_query($success, $query);</code> where <code>$result</code> is the variable for establishing connection to database.
   However, this isn't working and neither is <code>$result = mysqli_query($query);</code></p>
  
  <p>I need help in figuring out how to correctly pass a variable as query. </p>